Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在getJSON()jquery中处理404错误请求_Jquery_Rest_Jsonp - Fatal编程技术网

在getJSON()jquery中处理404错误请求

在getJSON()jquery中处理404错误请求,jquery,rest,jsonp,Jquery,Rest,Jsonp,我有以下代码,正在使用JSONP从站点检索JSON。我想处理错误代码,比如404错误请求。以下内容对我不起作用 $.getJSON('https://xyz.com/search?jsonp-callback=?', function(data) { alert("success"); }) .success(function() { alert("success 2"); }) .error(function() { alert("error occurred "); }) .compl

我有以下代码,正在使用JSONP从站点检索JSON。我想处理错误代码,比如404错误请求。以下内容对我不起作用

$.getJSON('https://xyz.com/search?jsonp-callback=?', function(data) {

  alert("success");
})
.success(function() { alert("success 2"); })
.error(function() { alert("error occurred "); })
.complete(function() { alert("Done"); });
成功和完整的方法有效,但错误方法无效

尝试而不是
错误
(请参阅)


以上代码可能对您有所帮助

此线程有以下答案:


由于您使用的是JSONP,因此不会调用jQuery创建的回调,因此不会调用失败回调。这是JSONP的正常行为,唉。

尝试在$.ajax..@DipeshParmar中测试相同的功能,但不起作用。您使用哪个版本的jQuery?@JacoKoster jQuery 1.8.2
$.getJSON('https://xyz.com/search?jsonp-callback=?', function(data) {
  alert("success");
})
.success(function() { alert("success 2"); })
.error(function(event, jqxhr, exception) {
    if (jqxhr.status == 404) {
              alert("error occurred ");   
    }
})
.complete(function() { alert("Done"); });