mcgrailm
mcgrailm

Reputation: 17640

jQuery AJAX function bizarre behavior

I have a rather odd scenario here, hoping someone might have some insight.

I have this function:

function get_jsonp(data){
    $.ajax({
        dataType: 'jsonp',
        data:data,
        jsonpCallback:'pr',
        url:'http://example.com/api/'
     });
//alert("test");
}

$("a").live('click',function(e){
    $("#dialog").show();
    $(".hideable").hide();
    window.location = $(this).attr('href');
    info = get_info();
    get_jsonp(info,'pr');
});

Under jQuery 1.4.1 this works consistently well.

Under jQuery 1.7.1 the first time its called there is no problem then when i click items that call it i get no results BUT if i un-comment that alert it works fine

any thoughts ?

Upvotes: 0

Views: 70

Answers (1)

Devin Burke
Devin Burke

Reputation: 13820

As soon as the ajax post is submit to the server, the method exits if you are using synchronous requests. What is happening is that your code that comes after get_jsonp() executes before the ajax call is actually complete. By having the alert(), you add just enough time before the method exits for the call to complete.

Unfortunately, jsonp doesn't support synchronous requests.

Consider using the .ajaxComplete() method or, if you can execute your subsequent code inside the get_jsonp method, catching one of the events:

function get_jsonp(data){
    $.ajax({
        dataType: 'jsonp',
        data:data,
        jsonpCallback:'pr',
        url:'http://example.com/api/',
        success: function(){
            // Do something when successfully complete.
        }
    });
}

Upvotes: 2

Related Questions