Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.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
Javascript 处理ajax错误_Javascript_Jquery_Ajax_Json - Fatal编程技术网

Javascript 处理ajax错误

Javascript 处理ajax错误,javascript,jquery,ajax,json,Javascript,Jquery,Ajax,Json,如何处理当前$的错误。getJSON?我认为使用getJSON没有直接选项,而是使用ajax方法: $.getJSON("test.php", function(json) { ... this function handles success }); 查看jQuery.ajax函数的 它将允许您创建带有错误和成功回调的GET请求 例如: $.ajax({ url: "test.php", dataType: "json", data: data, success: fu

如何处理当前
$的错误。getJSON

我认为使用getJSON没有直接选项,而是使用ajax方法:

$.getJSON("test.php", function(json) {
   ... this function handles success
});
查看
jQuery.ajax
函数的

它将允许您创建带有
错误
成功
回调的GET请求

例如:

$.ajax({
  url: "test.php",
  dataType: "json",
  data: data,
  success: function(data){
  },
  error: function(data){
   //ERROR HANDLING
  }
});
两种选择:

jQuery.ajax({
   url:     "test.php",
   type:    "GET",
   success: function() { //... },
   error  : function() { //... }     
});
或者您可以切换到$.ajax并使用
error
属性:

$.ajaxError(function(){});

为什么不这样做,这样您就可以专门处理错误了

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback,
  error: function(){
  }
});

或者您可以使用全局ajaxError

您使用的是哪个版本的jQuery?最新版本(1.5)为您提供了处理错误的新功能。有关新方法的更多信息,请参阅发行说明或
deferred.fail()
。我用的是最后一个稳定的-1.4。2@Happy请查看文档。$。getJSON只是一个简写的Ajax函数,相当于:我已经尝试过了,它不会从响应中给出任何答案
$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: function(data){}
  error: function(data){}
});