附加到Json的jQuery AJAX返回数据“d:null” [英] jQuery AJAX appending to Json return data "d:null"
问题描述
当我使用AJAX POST方法来调用我的webservice时,我得到了回调的奇怪回报。
Hey all I am getting an odd return from my callback when using the AJAX POST method to call my webservice.
webservice正在发回JSON,如下所示:
The webservice is sending the JSON back like this:
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 确定但仍然进入 ERROR 逻辑错误消息:
And for some reason my ajax returns 200 OK yet still goes into the ERROR logic with an error message of:
ERROR: "[{\"Column1\":\"0589S2F\"},{\"Column1\":\"53699FNS\"},{\"Column1\":\"C38VVFD\"},{\"Column1\":\"LFD55F\"},{\"Column1\":\"2ERfG\"},{\"Column1\":\"0079\"},{\"Column1\":\"2054\"},{\"Column1\":\"054FGW\"}]"{"d":null}
注意它如何将 {d:null} 附加到我的请求的末尾......自从我来自哪里我没有在 strResponse 中发送任何类似的内容???
Notice how it appends the {"d":null} to the end of my request... Where is that coming from since I am not sending anything like that back in the strResponse???
我的ajax请求代码:
My ajax request code:
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);
}
});
推荐答案
好吧,我只需要添加 Context.Response.Flush()在写入页面之前。
Well it would seem that I just needed to add Context.Response.Flush() before writing to the page.
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();
一旦我这样做,一切都很好。
Once I did that everything was fine.
这篇关于附加到Json的jQuery AJAX返回数据“d:null”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!