Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery ajax callback receiving incorrect json result
    text
    copied!<p>In my asp.net mvc 4 web app my action in the controller seems to return a invalid data to the jquery ajax callback... The action is doing a ping to an IP and it is called several times from several ajax calls in order to do pings to several IPs.</p> <p>A piece of my action in the controller (below what is returned):</p> <pre><code> Response.ContentType = "application/json;charset=utf-8"; Response.StatusCode = (int)(packetsLost &lt; 4 ? HttpStatusCode.OK : HttpStatusCode.NotFound); Response.TrySkipIisCustomErrors = true; return new JsonResult() { ContentType = "application/json;charset=utf-8", Data = new { sent = 4, received = 4 - packetsLost, lost = packetsLost, percentLost = (int) (packetsLost / 4 * 100), responseStatus01 = statuses[0], responseStatus02 = statuses[1], responseStatus03 = statuses[2], responseStatus04 = statuses[3], responseMessage01 = responseStatuses[0], responseMessage02 = responseStatuses[1], responseMessage03 = responseStatuses[2], responseMessage04 = responseStatuses[3], rtt01 = rtt[0], rtt02 = rtt[1], rtt03 = rtt[2], rtt04 = rtt[3], ttl01 = ttl[0], ttl02 = ttl[1], ttl03 = ttl[2], ttl04 = ttl[3], minRtt = roundTripTimes.Count == 0 ? "0" : string.Format("{0}ms", roundTripTimes.Min()), maxRtt = roundTripTimes.Count == 0 ? "0" : string.Format("{0}ms", roundTripTimes.Max()), avgRtt = roundTripTimes.Count == 0 ? "0" : string.Format("{0}ms", roundTripTimes.Average()) }, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; </code></pre> <p>and jquery ajax call:</p> <pre><code>ajax({ url: '/Controller/Action/', type: 'POST' }).then(function (data) { var _data; try { _data = $.parseJSON(data); } catch (err) { alert("Something went bad!!!"); } }, function (data) { var _data; try { _var = $.parseJSON(data.responseText); } catch (err) { alert("Something went bad!!!"); } }); </code></pre> <p>1) When action returns http status code OK (200) data.responseText in the jquery ajax callback is empty so I convert data instead (that contains correct data) but for some reason it is crashing when converting using parseJSON(data): error catched says invalid character... I do not know why...</p> <p>2) When action return http status code NotFound (404) data.responseText sometimes (not always) is empty and statusText within data is "error" so neither I do not understand it....</p> <p>Action in the controller always is returning the correct data....</p> <p>Could someone help me to detect this error? or right me in the right direction....</p> <p><strong>UPDATED:</strong> From ie dev tools, in network, I have observed that some pings are cancelled and then when trying to process them it returns empty responseText to the ajax callback so when trying to parse it, it crashes. I have observed that If I set the timeout for ping to a lower value, for example, 750ms, it is working but if i use the default timeout for ping, that is, 5sec, then it does not work: some pings are cancelled and when trying to serve them they are returning empty responseText that cause it to crash when trying to parse in ajax callback.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload