Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronous equivalent way of $.getJSON

Tags:

jquery

I have this code sample which returns data in JSON format to put into slickGrid:

$.getJSON(url, function(data) { 
    $.each(data, function() { dataReturn.push(this); });
    ...
    myDataView.setItems(dataReturn); 
    ...
});

but I am looking for a synchronous way and I tried something like this:

var dataReturn = JSON.stringfy( $.ajax({url:"...", async: false }).responseText );
myDataView.setItems(dataReturn);

but seems it isn't really the same...what I have missed?

Thanks in advance Cheers Luigi

like image 549
Luigino Avatar asked Dec 05 '25 14:12

Luigino


1 Answers

Use $.ajax and set async : false like this

$.ajax({
        url:url, 
        dataType : 'json',
        async : false,
        success : function(data) { 
            $.each(data, function() { dataReturn.push(this); });
             ...
            myDataView.setItems(dataReturn); 
            ...
            }
});
like image 157
void Avatar answered Dec 07 '25 04:12

void



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!