我想为 jquery $.ajax 调用添加一些功能。具体来说,我想捕获 http 错误 403。我不想编辑 jquery JS 本身。我开始查看 .extend 但对这些示例感到有些困惑。
我可以使用 $.extend $.ajax() 来捕获 403 吗?
TIA 僵尸杀手。
你看过jQuery.ajaxSetup选项吗?还是在jQuery.ajaxError?
无论哪种方式,都不要编辑 jQuery 源代码。
Extend doesn't work like that. You might be better served by something like this:
jQuery.fn.myCustomAjax = function() {
// custom code here
jQuery.ajax(); // etc
};
The #1 major benefit of this is that it won't stuff with any external code (plugins) and will be much easier to work with when you upgrade jQuery.
Thanks Guys. The suggested solution wont work becuase I need to trap for specific http result codes and then "do something special" via javascript.
so I have added 5 lines of code to jquery core library (I know dont flame me). To solve this problem in the core I suggest the following parameter would be added to jquery core libs (new parameter is responseHandlers)
$.ajax(
responseHandlers: {
"403":function() { http403(); } ,
"500":function() { http500(); }
}
global scoped:
function http403()
{
}
function http500()
{
}