0

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(/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);
    }
};

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. I probably didn't word my questions properly but I wanted to know why it was necessary for Malsup to write his own function for retrieving JSON objects. What were the issue with the built in $.getJSON method that Malsup didn't like.
0

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

Comments

0

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.

1 Comment

It wasn't really the question I was asking but thanks for the reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.