2013-02-28 76 views
0

我試圖使用Web服務來返回FullCallendar資源請求資源的JSON列表:使WebService只返回純文本JSON?

這裏是Web服務類:

/// <summary> 
/// Summary description for CalendarServices 
/// </summary> 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService] 
public class CalendarServices : System.Web.Services.WebService 
{ 
    public CalendarServices() 
    { 
    } 

    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public string GetEmployees() 
    { 

     List<object> eventList = new List<object>(); 
     var emps = ResourceManager.GetAllEmployees(); 
     foreach (Employee e in emps) 
     { 
      eventList.Add(
      new 
      { 
       id = e.EmployeID.ToString(), 
       name = e.EmployeName 
      } 
     ); 

     } 



     JavaScriptSerializer js = new JavaScriptSerializer(); 
     string strJSON = js.Serialize(eventList); 
     return strJSON; 
    } 
} 

這裏是它被稱爲:

var calendar = $('#calendar').fullCalendar({ 
    header: { 
     left: 'prev,next today', 
     center: 'title', 
     right: '' 
    }, 
    defaultView: 'resourceNextWeeks', 
    numberOfWeeks: 5, 
    weekends: false, 
    editable: true, 
    selectable: true, 
    minTime: 8, 
    maxTime: 16, 
    refetchResources: true, 
    selectHelper: true, 
    resources: 'CalendarServices.asmx/GetEmployees' 
     , 
    events: [ 
     { 

我不知道爲什麼這不起作用。也許我不明白網絡服務?

在這個例子中,他們調用了與之相反的resources.php。

從本質上講,我想是,如果我去CalendarServices.asmx /裝getEmployees

我想看到這在我的瀏覽器:

[ 
{ 
"name":"Resource 2", 
"id":"resource2" 
}, 
{ 
"name":"Resource 1", 
"id":"resource1"} 
] 

僅僅是純文本就行了。目前,如果我在瀏覽器中嘗試使用此網址,它會崩潰。

我該怎麼辦?

感謝

崩潰:

Request format is unrecognized for URL unexpectedly ending in '/GetEmployees'. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/GetEmployees'. 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


[InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/GetEmployees'.] 
    System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +489333 
    System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212 
    System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +47 
    System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +226 
    System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +145 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 
+0

是否伴隨着行號的崩潰以及可能暗示問題出在哪裏的錯誤消息? – Pete 2013-02-28 16:42:45

+0

@Pete看我的編輯。 – user2043533 2013-02-28 16:44:03

+0

ASMX是一項傳統技術,不應該用於新開發。 WCF應該用於Web服務客戶端和服務器的所有新開發。一個暗示:微軟已經在MSDN上退役了[ASMX Forum](http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/threads)。 – 2013-02-28 17:29:37

回答

0

你能在提琴手檢查,看看是否你的請求類型是POST或GET,你可能必須堅持一個額外的位在你的代碼。即

[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]