最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery AJAX appending to Json return data "d:null" - Stack Overflow

programmeradmin5浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 10

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.

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());

发布评论

评论列表(0)

  1. 暂无评论