jquery - JSONP request successful, but returns no data -
i'm having trouble retrieving jsonp data 1 of sites. on site data being provided following mvc2 controller action:
public jsonresult jsonlist(string key) {     var consultants = rep.findall().where(c => c.isactive).select(c => new consultantjsonitem { firstname = c.firstname, surname = c.surname });      return json(consultants, "application/json"); } on site b, i'm using jquery retrieve json, this:
$.ajax({     url: 'http://www.sitea.com/controller/jsonaction/',     datatype: 'jsonp',     crossdomain: true,     success: function (json) {         alert("success"); // displays         alert(json); // empty     },     error: function (xhr, status, error) {         alert(status); // not called     } }); i can see in firebug console response completed correctly 200 code, , can see content length of response 11516 bytes, yet response tab empty , jquery not give me data work with.
can tell me why is?
note: site using jquery 1.4.2
you returning json not same jsonp:
return json(consultants, "application/json"); the fact set datatype: 'jsonp' on client half of work need do. second half on server.
checkout following answer illustrates how create custom jsonpresult use callback query string parameter wrap response jsonp.
so:
public class jsonpresult : actionresult {     private readonly object _obj;      public jsonpresult(object obj)     {         _obj = obj;     }      public override void executeresult(controllercontext context)     {         var serializer = new javascriptserializer();         var callbackname = context.httpcontext.request["callback"];         var jsonp = string.format("{0}({1})", callbackname, serializer.serialize(_obj));         var response = context.httpcontext.response;         response.contenttype = "application/json";         response.write(jsonp);     } } and then:
public actionresult jsonlist(string key)  {     var consultants = rep.findall().where(c => c.isactive).select(c => new consultantjsonitem { firstname = c.firstname, surname = c.surname });     return new jsonpresult(consultants); } and on client:
$.ajax({     url: 'http://www.sitea.com/controller/jsonaction/',     jsonp: 'callback',     datatype: 'jsonp',     success: function (json) {         alert(json);     } }); 
Comments
Post a Comment