heron
heron

Reputation: 3661

How to wait for response in $.post jQuery

PHP returns value with 1-2 second delay jQuery.post doesn't wait for response.

How do you think, is it possible to fix that problem and wait for response?

    $.post( sSource, aoData, function (data) {    
        oCache.lastJson = jQuery.extend(true, {}, data);
        if ( oCache.iCacheLower != oCache.iDisplayStart )
        {
            data.aaData.splice( 0, oCache.iDisplayStart-oCache.iCacheLower );
        }
        data.aaData.splice( oCache.iDisplayLength, data.aaData.length );
         abc(oCache);
        fnCallback(data); 
    },"json" );

Note the same function with get works well

    $.getJSON( sSource, aoData, function (json) { 
        /* Callback processing */
        oCache.lastJson = jQuery.extend(true, {}, json);

        if ( oCache.iCacheLower != oCache.iDisplayStart )
        {
            json.aaData.splice( 0, oCache.iDisplayStart-oCache.iCacheLower );
        }
        json.aaData.splice( oCache.iDisplayLength, json.aaData.length );

        fnCallback(json)
    } );

Upvotes: 6

Views: 32742

Answers (1)

Vitaly
Vitaly

Reputation: 714

$.post is asynchronous, you need to use $.ajax and set async to false, that way you will be able to wait for the response. You can read more about it here: http://api.jquery.com/jQuery.ajax/

Upvotes: 24

Related Questions