admin管理员组文章数量:1241160
Hey all I am getting an odd return from my callback when using the AJAX POST method to call my webservice.
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()
The value of strResponse above is:
"[{\"Column1\":\"0589S2F\"},{\"Column1\":\"53699FNS\"},{\"Column1\":\"C38VVFD\"},{\"Column1\":\"LFD55F\"},{\"Column1\":\"2ERfG\"},{\"Column1\":\"0079\"},{\"Column1\":\"2054\"},{\"Column1\":\"054FGW\"}]"
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}
Notice how it appends the {"d":null} to the end of my request... Where is that ing from since I am not sending anything like that back in the strResponse???
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);
}
});
Hey all I am getting an odd return from my callback when using the AJAX POST method to call my webservice.
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()
The value of strResponse above is:
"[{\"Column1\":\"0589S2F\"},{\"Column1\":\"53699FNS\"},{\"Column1\":\"C38VVFD\"},{\"Column1\":\"LFD55F\"},{\"Column1\":\"2ERfG\"},{\"Column1\":\"0079\"},{\"Column1\":\"2054\"},{\"Column1\":\"054FGW\"}]"
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}
Notice how it appends the {"d":null} to the end of my request... Where is that ing from since I am not sending anything like that back in the strResponse???
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);
}
});
Share
Improve this question
asked Oct 12, 2015 at 15:40
StealthRTStealthRT
10.6k41 gold badges194 silver badges363 bronze badges
4
-
1
So.. I don't know about your question, but I'd like to note that passing SQL queries from the client is a terrible idea. There's nothing to stop the user from opening the console or using fiddler to send any arbitrary statement (like maybe
DROP TABLE vwPSDAT
) to the server. – Jason P Commented Oct 12, 2015 at 15:56 -
1
When you use
dataType: 'json'
, jQuery automatically parses the response. You don't need to use jQuery.parseJSON. This isn't the problem, though, because you're not going into the
success` function. – Barmar Commented Oct 12, 2015 at 16:07 -
There must be some other part of the webservice that's writing
{"d": null}
. – Barmar Commented Oct 12, 2015 at 16:07 - You can only return one JSON object in a response. – Barmar Commented Oct 12, 2015 at 16:08
3 Answers
Reset to default 10Well 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.
Adding response.End() solved problem for me:
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();
In the case of doing what StealthRT seas, you got some error appended to the data you need, and you can get rid of it by specifying the length of your response and limiting the output:
Context.Response.AddHeader("content-length", strResponse.Length.ToString());
本文标签: javascriptjQuery AJAX appending to Json return data quotdnullquotStack Overflow
版权声明:本文标题:javascript - jQuery AJAX appending to Json return data "d:null" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740061119a2222612.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论