Jquery ajax successful result is zero

I am making an ajax call using jquery to get data in json format. the success callback function is called, but the data is empty.

$(document).ready(function () { $.ajax({ url: "http://apps.sungardhe.com/StudentResearch/public/Research.svc/Schools", type: "GET", contentType: "application/json; charset=utf-8", dataType: "json", success: cbSchools }); }); function cbSchools(data) { if (data == null) { alert("data is null"); return; } for (var school in data) { $("#ddSchool").append("<option value='" + data[school].ShortName + "'>" + data[school].ShortName + "</option>"); } } 

With fiddler, I can see that the answer actually returns json data, but for some reason the jquery result object is NULL. can anyone tell me why?

+4
source share
3 answers

You are blocked by a policy of the same origin that prevents cross-domain XMLHttpRequests. Since you need to configure headers to return JSON from a .Net web service like this, you are in a difficult place, you simply cannot make such a request from a browser, and not from another domain.

Fiddler may display content, but the browser is not going to view the page, because for security reasons it will always be zero. One way: JSONP , but, unfortunately, this service does not seem to be configured to support it.

+7
source

I believe that you can make your calls generic (the reason Marduk points out)

To handle this and make common calls (works with data and data.d), I use the following in my ajax calls (with my asp.net stuff) so that it works with old and new services:

  dataFilter: function(data) { var msg; if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') msg = JSON.parse(data); else msg = eval('(' + data + ')'); if (msg.hasOwnProperty('d')) return msg.d; else return msg; }, 

EDIT: IF it is really empty AND NOT "undefined", then the cross-domain problem can be reproduced here.

+1
source

try it

 if (data.d == null) { alert("data.d is null"); return; } 

since your return data type is json, the data is in the data, "d", a variable in the response object.

0
source

All Articles