javascript 即使出现错误,ajaxSubmit 也会调用成功回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17142826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:14:24  来源:igfitidea点击:

ajaxSubmit calls the success callback even when there is an error

javascriptajaxjquery

提问by maxlk

I'm using jquery.form.js and ajax to submit my forms. The problem I have is even when it's not on success (when there is an error) also the ajax is resetting the form no matter it is in code only to do it on after success.

我正在使用 jquery.form.js 和 ajax 来提交我的表单。我遇到的问题是,即使它没有成功(出现错误时),ajax 也会重置表单,无论它是否在代码中,只是在成功后才执行。

Here is the code:

这是代码:

<script>
$(document).ready(function()
{
    $('#FileUploader').on('submit', function(e)
    {
        e.preventDefault();
        $('#submit').attr('disabled', ''); // disable upload button
        //show uploading message
        $("#loadding").html('<div class="loader"><img src="images/ajax-loader.gif" alt="Please Wait"/> <br/><span>Submiting...</span></div>');
        $(this).ajaxSubmit({
            target: '#output',
            success:  afterSuccess //call function after success
        });
    });
});

function afterSuccess()
{
    $('#FileUploader').resetForm();  // reset form
    $('#submit').removeAttr('disabled'); //enable submit button
    $('#loadding').html('');
}
</script>

Can someone point out where is the problem. Thanks in advance.

有人可以指出问题出在哪里。提前致谢。

回答by Anshu Dahiya

$(this).ajaxSubmit({
            target: '#output',
            success:  afterSuccess,
            error:error
        });

function error(response,status,xhr){
alert("error");}

will work as i had implemented same concept in my project and it works fine.

将像我在我的项目中实施相同的概念一样工作,并且工作正常。

回答by palmplam

Did you change the http status code of the server response if an error occuring ?
For jQuery, success= 2xx http status code return in the response and error= 4xx
If you didn't change the header of response, for your jQuery code, it's always a success!!!

如果发生错误,您是否更改了服务器响应的 http 状态代码?
对于 jQuery,success响应中返回 = 2xx http 状态码和error= 4x​​x
如果您没有更改响应的标头,对于您的 jQuery 代码,它总是成功的!!!


So 2 solutions :


所以2个解决方案:

  1. You can change the http header code status if it's an error.
  2. You can return a different content in response and check it in your successfunction.
  1. 如果出现错误,您可以更改 http 标头代码状态。
  2. 您可以在响应中返回不同的内容并在您的success函数中检查它。

Other point: change the definition of the success property like behind.

其他一点:像后面一样改变成功属性的定义。

success:  function () { afterSuccess(); } //call function after success

Why ? Because, if you declare directly the function, it will be execute during the ajaxSubmit() configuration, maybe it's solve your problem, because before the response of the server, afterSuccess()will be called.

为什么 ?因为,如果你直接声明函数,它会在 ajaxSubmit() 配置期间执行,也许它可以解决你的问题,因为在服务器响应之前,afterSuccess()将被调用。

回答by MrCode

The successevent will always fire if the HTTP response code is in the 200-299 range (or === 304), anything else and it won't fire. So I'm guessing that your server side code responds with 200 OKeven when there is a problem.

success如果 HTTP 响应代码在 200-299 范围内(或 === 304),则该事件将始终触发,其他任何内容都不会触发。所以我猜你的服务器端代码200 OK即使出现问题也会响应。

If the server outputs a value such as trueyou can test the response text for that:

如果服务器输出一个值,例如true您可以为此测试响应文本:

    $(this).ajaxSubmit({
        target: '#output',
        success:  function(response) {
            if(response == "true") {
                afterSuccess();
            }
        }
    });