$.ajax() success called, but will not run method

This is really puzzling me. Any idea why this would be happening?

This code does work:

 $.ajax({
            type: 'GET',
            dataType: 'json',
            url: 'http://localhost:3235/Users/searchUsers?callback=?&searchString=' + searchString,
            success: alert("Success")
        });

This code does not:

 $.ajax({
            type: 'GET',
            dataType: 'json',
            url: 'http://localhost:3235/Users/searchUsers?callback=?&searchString=' + searchString,
            success: function(data){
              alert("Success");
            }
        });

Answers


The first snippet is bad syntax and shouldn't be used, you get the alert because it is called immediately, not on success.

There are only two reasons for you to not get the alert with the 2nd snippet.

  1. the server isn't returning a success status code, for example, it's either 404, 500, etc, not 200.
  2. The only other possibility is the jsonp being returned either isn't jsonp, or isn't valid jsonp.

Most likely you are confusing JSONP with JSON, so i'll give you an example of each. First is json:

{"foo":"bar"}

and this is JSONP ...?callback=somecallbackname&...:

somecallbackname({"foo":"bar"})

Note, somecallbackname will be supplied by jQuery, you'll have to get the value of the callback get parameter and use it to generate the JSONP accordingly.


Need Your Help

How to pass additional extra arguments to a batch file?

batch-file command-line-arguments

I would like to use first 2 arguments passed to a batch file and then append everything else to the command executed in the batch file.

rewrite non www to www except subdomain rails 3 and rack

ruby-on-rails rack rack-middleware

I need to be able to rewrite non www to www but NOT in the case when there is a (non www) subdomain present.

Is leaving a procedure with a return statement a bad thing?

function design return

I sometimes have colleagues frowning about using the return statement to leave a routine (before the end) because it looks like a kind of hidden goto and all gotos are evil. I neither share the tho...