AjaxSetup从不执行成功函数

我有一个简单的注册页面,可以validation用户是否已被带走。

我对所有的ajax调用使用ajaxSetup,并且出于某种原因,永远不会调用“success”。 当我查看我的控制台(firebug)时,我可以看到一个成功的请求(代码200 OK,结果我得到了真或假)。

这是我的代码:

$('#checkValidUsername').click(function() { // some basic validation like not empty etc... $.ajax({ type: "POST", url: '/checkuser.php', cache: false, data: $("#form").serialize(), dataType: 'json', success: function(result) { // do some actions }, }); } $.ajaxSetup({ beforeSend: function() { // show loading dialog // works }, complete: function(xhr, stat) { // hide dialog // works } success: function(result,status,xhr) { // not showing the alert alert('success'); } }); 

我的代码出了什么问题? 谢谢

因为您已在$.ajax调用中使用success

 $('#checkValidUsername').click(function() { // some basic validation like not empty etc... $.ajax({ type: "POST", url: '/checkuser.php', cache: false, data: $("#form").serialize(), dataType: 'json' /* success: function(result) { // do some actions }, */ }); } $.ajaxSetup({ beforeSend: function() { // show loading dialog // works }, complete: function(xhr, stat) { // hide dialog // works } success: function(result,status,xhr) { // not showing the alert alert('success'); } }); 

$.ajax所有内容都将覆盖$.ajaxSetup

删除$.ajax()success处理程序。

 $('#checkValidUsername').click(function() { // some basic validation like not empty etc... $.ajax({ type: "POST", url: '/checkuser.php', cache: false, data: $("#form").serialize(), dataType: 'json', success: function(result) { myAjaxSetup.success.apply(this, arguments); // do some actions }, }); } var myAjaxSetup = { beforeSend: function() { // show loading dialog // works }, complete: function(xhr, stat) { // hide dialog // works } success: function(result,status,xhr) { // not showing the alert alert('success'); } }; $.ajaxSetup(myAjaxSetup); 

您简单覆盖的每个对象函数

 myAjaxSetup.success.apply(this, arguments); 

要么

 myAjaxSetup.error.apply(this, arguments); 

要么

 myAjaxSetup.anyfunctionyouwant.apply(this, arguments); 

当您在ajax调用中指定success函数时,我认为您将覆盖它。 尝试删除它,看看它是否从ajaxSetup调用了一个。

$.ajaxSetup()是一种提供适用于所有未来ajax()调用的预制默认设置的方法,除非您在特定的ajax()调用中覆盖它们。 在ajaxSetup()调用和ajax()调用本身中定义成功处理程序时,只会调用其中一个成功处理程序。

因此,如果您希望从ajaxSetup()调用成功处理程序,则不要在ajax()调用中定义一个。 如果在ajax()调用中定义一个,则不会调用ajaxSetup()的一个。

您可以使用全局Ajax事件处理程序

 $(document).ajaxSuccess(function() { $( ".log" ).text( "Triggered ajaxSuccess handler." ); });