让WebService只返回纯文本JSON

本文关键字:文本 JSON 返回 WebService | 更新日期: 2023-09-27 18:01:48

我正在尝试使用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: [
        {

我不知道为什么这是不工作。也许我不了解web服务?

在这个例子中,他们调用resources.php,它只是回显它。

我想要的是如果我去到calendarservices。asmx/GetEmployees

我想在我的浏览器中看到这个:

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

纯文本就可以了。目前,如果我在浏览器中尝试这个url,它会崩溃。

我能做什么?

感谢

崩溃:

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

让WebService只返回纯文本JSON

您是否可以检查fiddler以查看您的请求类型是POST还是GET,您可能必须在代码中添加额外的位。即

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