【发布时间】: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 thesuccess` 函数。 -
网络服务中一定有其他部分在写
{"d": null}。 -
响应中只能返回一个 JSON 对象。
标签: javascript jquery json ajax web-services