> I've coded a drop-in replacement for getJSON that notifies when the url
> cannot be reached or the response was not well-formed:
>
> http://code.google.com/p/getjson/


This is what I'm using (big hat tip to Ricardo for working out the
timeout logic!):


// fn to handle jsonp with timeouts and errors
$.getJSONP = function(s) {
    s.dataType = 'jsonp';
    $.ajax(s);

    var t = 0, cb = s.url.match(/callback=(\w+)/)[1], cbFn = window
[cb];
    var $script = $('head script[src*='+cb+']');
    if (!$script.length)
        return; // same domain request

    $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);
};

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to