【问题标题】:jQuery AJAX appending to Json return data "d:null"jQuery AJAX 附加到 Json 返回数据“d:null”
【发布时间】:2016-01-10 03:56:39
【问题描述】:

大家好,当我使用 AJAX POST 方法调用我的 web 服务时,我的回调得到了一个奇怪的返回。

Web 服务正在这样发送 JSON:

Dim ser As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim strResponse As String = ser.Serialize(results)

Context.Response.Clear()
Context.Response.ContentType = "application/json"
Context.Response.AddHeader("content-length", strResponse.Length.ToString())
Context.Response.Write(strResponse)
HttpContext.Current.ApplicationInstance.CompleteRequest()

上述strResponse的值为:

"[{\"Column1\":\"0589S2F\"},{\"Column1\":\"53699FNS\"},{\"Column1\":\"C38VVFD\"},{\"Column1\":\"LFD55F\"},{\"Column1\":\"2ERfG\"},{\"Column1\":\"0079\"},{\"Column1\":\"2054\"},{\"Column1\":\"054FGW\"}]"

由于某种原因,我的 ajax 返回 200 OK 但仍然进入 ERROR 逻辑,并显示以下错误消息:

ERROR: "[{\"Column1\":\"0589S2F\"},{\"Column1\":\"53699FNS\"},{\"Column1\":\"C38VVFD\"},{\"Column1\":\"LFD55F\"},{\"Column1\":\"2ERfG\"},{\"Column1\":\"0079\"},{\"Column1\":\"2054\"},{\"Column1\":\"054FGW\"}]"{"d":null}

注意它是如何将 {"d":null} 附加到我的请求末尾的......这是从哪里来的,因为我没有在 中发送类似的东西strResponse???

我的ajax请求代码:

var sqlQ = "SELECT TOP 50 LTRIM(RTRIM(REPLACE(OID, ' ', ''))) FROM vwPSDAT WHERE OID != ''";

$.ajax({
     type: 'POST',
     contentType: 'application/json; charset=utf-8',
     dataType: 'json',
     url: 'http://zzzz/Service1.asmx/theQ',
     data: JSON.stringify({ qString: [sqlQ] }),
     async: true,
     cache: false,
     success: function (data) {
        var obj = jQuery.parseJSON(data);
        console.log('done!');                  
     },
     error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.log('ERROR: ' + XMLHttpRequest.responseText);
     }
});

【问题讨论】:

  • 所以.. 我不知道你的问题,但我想指出从客户端传递 SQL 查询是一个糟糕的主意。没有什么可以阻止用户打开控制台或使用 fiddler 向服务器发送任意语句(可能是DROP TABLE vwPSDAT)。
  • 当你使用dataType: 'json'时,jQuery会自动解析响应。你不需要使用 jQuery.parseJSON. This isn't the problem, though, because you're not going into the success` 函数。
  • 网络服务中一定有其他部分在写{"d": null}
  • 响应中只能返回一个 JSON 对象。

标签: javascript jquery json ajax web-services


【解决方案1】:

看来我只需要在写入页面之前添加 Context.Response.Flush()

Context.Response.Clear();
Context.Response.ContentType = "application/json";
Context.Response.AddHeader("content-length", strResponse.Length.ToString());
Context.Response.Flush();
Context.Response.Write(strResponse);
HttpContext.Current.ApplicationInstance.CompleteRequest();

一旦我这样做了,一切都很好。

【讨论】:

  • 写入后应该调用flush
  • 您似乎更改为[ScriptMethod(UseHttpGet = true)] 并开始使用GET?因为这对我不起作用
  • 对我没用。添加 AddHeader 后,我在邮递员中收到此“错误:解析错误:预期的 HTTP/”
【解决方案2】:

在执行 StealthRT seas 的情况下,您在需要的数据中附加了一些错误,您可以通过指定响应的长度并限制输出来摆脱它:

Context.Response.AddHeader("content-length", strResponse.Length.ToString());

【讨论】:

    【解决方案3】:

    添加 response.End() 为我解决了问题:

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ContentType = "application/json";
    HttpContext.Current.Response.AddHeader("content-length", res.Length.ToString());
    HttpContext.Current.Response.Write(res);
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.End();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多