Error handling in getJSON calls

后端 未结 9 1659
野的像风
野的像风 2020-11-28 02:09

How can you handle errors in a getJSON call? Im trying to reference a cross-domain script service using jsonp, how do you register an error method?

9条回答
  •  误落风尘
    2020-11-28 02:21

    Why not

    getJSON('get.php',{cmd:"1", typeID:$('#typesSelect')},function(data) {
        // ...
    });
    
    function getJSON(url,params,callback) {
        return $.getJSON(url,params,callback)
            .fail(function(jqXMLHttpRequest,textStatus,errorThrown) {
                console.dir(jqXMLHttpRequest);
                alert('Ajax data request failed: "'+textStatus+':'+errorThrown+'" - see javascript console for details.');
            })
    }
    

    ??

    For details on the used .fail() method (jQuery 1.5+), see http://api.jquery.com/jQuery.ajax/#jqXHR

    Since the jqXHR is returned by the function, a chaining like

    $.when(getJSON(...)).then(function() { ... });
    

    is possible.

提交回复
热议问题