beforeSend in $.getJSON

callback, javascript, jquery

Solution

The only purpose of beforeSend is to get at the raw XHR object (normally for setting HTTP headers on it). You don't need it for kicking off spinners and the like. This code here (from @petersendidit):

jQuery.ajax({
    url: url,
    dataType: "json",
    beforeSend: function(){
        $('.loading').show();
    }
});

Is better written like this:

$('.loading').show();
jQuery.ajax({
    url: url,
    dataType: "json"
});

Which means, unless you need any advanced options in jQuery.ajax, your original plan to use jQuery.getJSON is just fine. So you say you want to show a loading GIF, just do this and forget about `beforeSend`.

jQuery(".someSpinnerImage").show();
jQuery.getJSON("http://www.somedomain.com/someurl", function(data) {
    jQuery(".someSpinnerImage").hide();
    // Do something with data
}

Problem

How do I use `beforeSend` callback in `$.getJSON`(cross domain). More specifically `$.getJSON` is call is to a YQL service Like select * from html where url=”http://www.yahoo.com”

Original source