2009-12-20 48 views
1

我在我的代码中执行“$ .ajax”调用的许多地方 我可以定义一个地方来处理所有错误吗?jquery ajax error

我知道有一个“错误:函数...”我可以在每个呼叫的地方,但是我想写它只是一个时间

回答

1
$.ajaxSetup({}); 
+3

非常非常...简洁。 @OP:它为Ajax请求设置默认选项。链接:http://docs.jquery.com/Ajax/jQuery.ajaxSetup#options – 2009-12-20 15:23:39

+0

你有一个示例代码? – haddar 2009-12-20 15:28:41

+0

$ .ajaxSetup({ 错误:函数(XMLHttpRequest参数url errorThrown){ 警报(XMLHttpRequest的+ ':' + textStatus + ':' + errorThrown); 将返回false;} }); 是可以吗? – haddar 2009-12-20 15:34:25

0

您可以只定义一次错误处理函数,然后在Ajax调用中引用它,而不是在每个调用中使用函数文字。

1

你应该使用$ .ajaxSetup()事件处理方法(由JQuery的文档说明):

http://api.jquery.com/jQuery.ajaxSetup/

Note: Global callback functions should be set with their respective global Ajax event handler methods—.ajaxStart(), .ajaxStop(), .ajaxComplete(), .ajaxError(), .ajaxSuccess(), .ajaxSend()—rather than within the options object for $.ajaxSetup().

使用$ .ajaxError()方法相反:

http://api.jquery.com/ajaxError