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.

  1. the server isn't returning success status code, example, it's either 404, 500, etc, not 200.
  2. 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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -