2010-11-10 6 views
0

J'ai besoin de reformater la fonction ci-dessous sous la forme d'un appel jQuery.ajax afin de gérer les erreurs (le script semble arriver à expiration sur certains appels). À quoi cela ressemblerait-il s'il était converti en .ajax() avec un callback d'erreur et un callback réussi?Aidez-nous à convertir jQuery.getJson en jQuery.ajax()

jQuery.getJSON("http://boss.yahooapis.com/ysearch/web/v1/tobacco" 
    +"appid=myAppID" 
    +"&lang=en" 
    +"&format=json" 
    +"&count=50" 
    +"&view=keyterms" 
    +"&callback=?", 
    function (data) { 
     // if no error, do something, else gracefully exit 
    }); 
+0

Vous pouvez utiliser '.ajaxError'. http://api.jquery.com/ajaxError/ –

+0

qui vous donnera un gestionnaire d'erreur globale, en utilisant une erreur: func – dstarh

Répondre

2
$.ajax({ url: "http://boss.yahooapis.com/ysearch/web/v1/tobacco" 
    +"appid=myAppID" 
    +"&lang=en" 
    +"&format=json" 
    +"&count=50" 
    +"&view=keyterms" 
    +"&callback=?", 
    success: function(data){ 
     //do somethign with the data 
     }, 
    error:function(XMLHttpRequest, textStatus, errorThrown){ 
     //do something on error 
    } 
    }); 
+0

où voulez-vous insérer l'erreur: function() " –

+0

jQuery erreur fonction ne fonctionne pas bien tout le temps avec JQuery 1.4, pour une implémentation fiable, utilisez try/catch (juste au cas où vous auriez besoin de gérer les erreurs) – t0mcat

+0

@ t3ch: Puis-je utiliser try/catch avec mon code original dans ma question (vs convertir en .ajax comme FatherStorm le suggère? –

0

Je sais qu'il est 5 ans plus tard, mais vous ne devriez probablement pas construirons votre propre chaîne de requête

$.ajax({ url: "http://boss.yahooapis.com/ysearch/web/v1/tobacco", 
    data: {appid: "myAppID", 
     lang: "en", 
     format: "json", 
     count: "50", 
     view: "keyterms", 
     callback: "?"}, 
    type: "GET", 
    dataformat: "JSON", 

    success: function(data){ 
     //do something with the data 
    }, 

    error:function(XMLHttpRequest, textStatus, errorThrown){ 
     //do something on error 
    } 
}); 
Questions connexes