Search code examples
jqueryjsonp

in jquery.get for JSONP how to set async false


I am hitting a timing issue with ajax calls in JQuery using jsonp. I need to wait for my server call to return before proceeding further. However in http://api.jquery.com/jQuery.get/ there appears to be no place for setting async. Here is my code:

        what = ZModel;
        jQuery.support.cors = true;
        var req = $.get( url, qp, function( stuff ) {
            if ( stuff.status == "OK" ) {
                what.metadata[i].model_name = stuff.datarows;
            } else {
                console.log("...LOAD QUERY ERROR: " + stuff.errtext + "; qs: " + thing.qs);
            }
            alert("success in get");
        }, 'jsonp');
        req.error( function() {
            console.log("...LOAD AJAX ERROR: " + JSON.stringify(arguments));
        });

I also tried $.ajax but the code does not hit success. The manpage states that clearly but I couldn't figure out an alternative.

Is there a different way to accomplish the same?

Thanks


Solution

  • It is completely impossible to do synchronous JSONP. JSONP works by creating a <script> tag; the browser does not expose any way to wait for a <script> tag.

    In addition, SJAX is never a good idea; it will freeze the browser until the server replies.

    You need to correctly use asynchrony.
    Consider using promises.