$.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

Issue in kaminari ajax pagination

ajax ruby-on-rails-3 pagination kaminari

Am using kaminari ajax pagination in my project. It is working fine, But contents are displaying number of pages times in the page. For example if number of items per pages is 7, then it is display...

Perl Mime::Lite hash of array derefrence

perl hash mime

I have a array of hash which I am dereferencing in the msg->attach() function of the Mime::Lite. The code is as follows:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.