通过ajax请求url获取错误响应的问题

时间:2011-07-25 07:03:21

标签: jquery ajax

$.ajax({
   url: "http://192.168.1.9:8983/solr/db/select/?qt=dismax&wt=json&&start=0&rows=10&q=ring&json.wrf=?",
    type: 'GET',
    crossDomain:'true',
    dataType:'json',
    complete: function(jqXHR,textStatus) {

                        alert(textStatus);

         }
    })

如果url是正确的,我得到success但如果url不正确则假设ip错误,则完成函数不会执行。比如何确定网址是错误的。

2 个答案:

答案 0 :(得分:0)

使用函数error(),这将处理所有类型的错误。

示例:

$.ajax({
   url: "http://192.168.1.9:8983/solr/db/select/?qt=dismax&wt=json&&start=0&rows=10&q=ring&json.wrf=?",
   type: 'GET',
   crossDomain:'true',
   dataType:'json',
   complete: function(jqXHR,textStatus) {
       alert(textStatus);
   },
   error(jqXHR, textStatus, errorThrown){
       alert("Error : " + textStatus);
   } 
});

答案 1 :(得分:0)

错误处理就像成功处理一样:

$.ajax({
   url: "http://192.168.1.9:8983/solr/db/select/?qt=dismax&wt=json&&start=0&rows=10&q=ring&json.wrf=?",
   type: 'GET',
   crossDomain:'true',
   dataType:'json',
   complete: function(jqXHR,textStatus) {
       alert(textStatus);
   },
   success:function(data){
      // handle data object
   },
   error:function(jqXHR, textStatus, errorThrown){
       alert("Error : " + textStatus);
   } 
});

Talha有正确的想法,但他的语法不正确。