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

Eclipse formatter for java ending braces -

ruby - Nesting modules inside of a Rails eninge gem -

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -