javascript - $.ajax() success called, but will not run method -
this puzzling me. idea why happening?
this code work:
$.ajax({ type: 'get', datatype: 'json', url: 'http://localhost:3235/users/searchusers?callback=?&searchstring=' + searchstring, success: alert("success") });
this code not:
$.ajax({ type: 'get', datatype: 'json', url: 'http://localhost:3235/users/searchusers?callback=?&searchstring=' + searchstring, success: function(data){ alert("success"); } });
the first snippet bad syntax , shouldn't used, alert because called immediately, not on success.
there 2 reasons not alert 2nd snippet.
- the server isn't returning success status code, example, it's either 404, 500, etc, not 200.
- the other possibility jsonp being returned either isn't jsonp, or isn't valid jsonp.
most confusing jsonp json, i'll give example of each. first json:
{"foo":"bar"}
and jsonp ...?callback=somecallbackname&...
:
somecallbackname({"foo":"bar"})
note, somecallbackname
supplied jquery, you'll have value of callback parameter , use generate jsonp accordingly.
Comments
Post a Comment