使WebService只返回纯文本JSON?

时间:2013-02-28 16:40:15

标签: c# asp.net json web-services

我正在尝试使用Web服务返回FullCallendar资源请求的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

1 个答案:

答案 0 :(得分:0)

你可以检查fiddler以查看你的请求类型是POST还是GET,你可能需要在代码中多加一点。即

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