Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery AJAX appending to Json return data "d:null"

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 coming 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);
     }
});
like image 953
StealthRT Avatar asked Oct 12 '15 15:10

StealthRT


1 Answers

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.

like image 52
StealthRT Avatar answered Sep 21 '22 12:09

StealthRT