开发者

Using jQuery's getJSON() method

开发者 https://www.devze.com 2023-01-10 02:14 出处:网络
So I was reading through the code on Malsup\'s twitter plugin and I noticed he\'d written his own method to handle jsonp but with timeouts and errors. I can only assume the built injQuery method \'get

So I was reading through the code on Malsup's twitter plugin and I noticed he'd written his own method to handle jsonp but with timeouts and errors. I can only assume the built in jQuery method 'getJSON' doesn't have this functionality even though it clearly works fine.

So, should I continue to use Malsups version in my projects where I'm making JSONP requests or just stick with jQuery's method. I have emailed Malsup and Paul Irish to ask about why it was necessary to write this but I didn't hear back. Can't blame 'em really:)

$.getJSONP = function(s){
    s.dataType = 'jsonp';
    $.ajax(s);

    // figure out what the callback fn is
    var $script = $(document.getElementsByTagName('head')[0].firstChild);
    var url = $script.attr('src') || '';
    var cb = (url.match(/开发者_JS百科callback=(\w+)/) || [])[1];
    if (!cb) 
        return; // bail
    var t = 0, cbFn = window[cb];

    $script[0].onerror = function(e){
        $script.remove();
        handleError(s, {}, "error", e);
        clearTimeout(t);
    };

    if (!s.timeout) 
        return;

    window[cb] = function(json){
        clearTimeout(t);
        cbFn(json);
        cbFn = null;
    };

    t = setTimeout(function(){
        $script.remove();
        handleError(s, {}, "timeout");
        if (cbFn) 
            window[cb] = function(){
            };
    }, s.timeout);

    function handleError(s, o, msg, e){
        // support jquery versions before and after 1.4.3
        ($.ajax.handleError || $.handleError)(s, o, msg, e);
    }
};


If it's JSONP, you can use

$.getJSON(url + "&callback=?", [args]);

to get JSONP and call a function when it loads. The &callback=? query lets jQuery generate a random callback function in the global scope to respond to the JSONP call.


From the jQuery docs for getJSON(...)

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript's object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes. For details on the JSON format, see http://json.org/.

Presumably, silent failure was not something they liked, hence the plugin. In your case, if you're making JSONP requests and find yourself using the onError, or onTimeout methods, then keep the plugin. I've not had to use JSONP in any real capacity, but I would assume that error handling is always nice to have. In the link to the jQuery docs, there is good discussion on this towards the end of the comments


JQuery HowTo has a post specifically about using jQuery, JSONP, and Twitter.

Twitter JSON(P) API URL:

http://twitter.com/status/user_timeline/USERNAME.json?count=10&callback=JSONPcallbackFunction

Here is a code to use with jQuery’s $.getJSON() function:

http://twitter.com/status/user_timeline/USERNAME.json?count=10&callback=?

We have put ? (question mark) for callback function name so jQuery could replace it with a dynamic one that it has created.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号