ASP.NET AJAX返回JSON但未被识别为JSON [英] ASP.NET AJAX returning JSON but not recognised as JSON

查看:112
本文介绍了ASP.NET AJAX返回JSON但未被识别为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有此功能,可以让我重新获得经理列表

I have this function to get me back a list of managers

function getManagers() {
   var jqxhr = $.ajax({
                   type: 'POST',
                   contentType: "application/json; charset=utf-8",
                   url: '/webservice.asmx/GetManagers',
                   dataType: 'json'
               }).success(function(data) {
                   var options = '<option selected="selected" disabled="disabled">Select Manager</option>';
                   for (var i = 0; i < data.length; i++) {
                       options += '<option value="' + data[i].PostRef + '">' + data[i].Description + '</option>';
                   }
                   $('#ReceivingCellManager').html(options);
               }).error(function(data) {
                   $('.ErrorText').html('Manager load failed please refresh page with F5');
                   $("#errormessage").dialog('open');
               }).complete(function() {
           });
} 

如您所见,我正在使用JQuery,并希望使用可用的管理器填充下拉列表

as you can see I am using JQuery and want to popoulate a drop down list with the available managers

我的服务中的方法如下

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void GetManagers()
    {
        using (var context = new ConcessionModel())
        {             
            var rcm = Business.GetManager();
            var serializer = new JavaScriptSerializer();
            var response = rcm.Count() != 0
                ? serializer.Serialize(rcm)
                : serializer.Serialize(new Error { Code = "500", Message = "Manager Retrieval Failed" });
            this.Context.Response.Clear();
            this.Context.Response.ContentType = "application/json";
            this.Context.Response.Write(response);
        }
    }

调用该方法时,我收到的响应为 200 OK ,并且该响应包含JSON,我希望我遇到的问题是该响应未被识别为JSON.

When the method is called I recieve a response of 200 OK and the response contains the JSON I want the problem I am having is that the response is not being recognised as JSON.

我已经尝试过

    如上所示,
  • dataType 添加到ajax调用中
  • 从响应末尾删除 this.Context.Response.flush ,这解决了我在发送标头后调整标头时遇到的错误.
  • 向该方法添加响应格式
  • 向上下文添加 Response.ContentType 这些都无法让我获得所需的JSON识别.任何帮助将不胜感激.
  • adding dataType to the ajax call as you can see above
  • removing this.Context.Response.flush from the end of the response, this cured an error I was getting with adjusting headers after they were sent.
  • adding a response format to the method
  • adding a Response.ContentType to the Context These have all failed to get me the required recognition of the JSON. Any help would be much appreciated.

更新:JSON格式

{说明":数据",代码":数据",参考":数据"}

{"Description":"data","Code":"data","reference":"data"}

更新JSON响应 我在回复中看到一些奇怪的内容,如下所示

UPDATE JSON RESPONSE I am seeing something weird in the response my response is as follows

[{"Description":"data","Code":"data","reference":"data"}]{"d":null}

我不确定 d空对象是什么

推荐答案

为了从服务访问json对象(例如"Dog"类的对象),方法的返回类型应为"Dog".不要在服务中使用response.write.然后,您可以使用"data.d"从成功消息中访问数据.

In order to access the json object such as an object of class 'Dog' from the service, the method return type should be 'Dog'. Do not use response.write in services. You can then access the data from the success message using 'data.d'.

希望这会有所帮助!

这篇关于ASP.NET AJAX返回JSON但未被识别为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆