简体   繁体   English

jQuery post回调函数失败,并且总是被调用完成

[英]Jquery post callback function fail and done always being called

I have an API controller that returns a HttpStatusCodeResult 200 if it worked and 500 if they weren't able to register. 我有一个API控制器,如果它工作正常,则返回HttpStatusCodeResult 200;如果他们无法注册,则返回500。 The problem is .done and .fail will both be called no matter what status code is returned. 问题是无论返回什么状态码,.done和.fail都将被调用。 However the information is posted or not posted correctly. 但是,该信息已发布或未正确发布。 Here is my post function. 这是我的帖子功能。 Any ideas what could be causing this would be greatly appreciated? 任何想法可能导致这种情况将不胜感激?

function register() {
    $.post("../api/Register",
                {
                    'Email': $("#rEmail").val(),
                    'Password': $("#rPassword").val()
                })
    .done((function () {
        alert("Thank you for registering!");
    })())
    .fail((function () {
        alert("Email already exists");
    })());

}

Edit: The problem is that it was reloading the page when jquery.unobtrusive is supposed to prevent that from happening. 编辑:问题是,当jquery.unobtrusive应该防止此情况发生时,它正在重新加载页面。 The fix or workaround was changing it to a button and not a form. 该修复程序或解决方法将其更改为按钮而不是表单。

Instead of passing the anonymous functions you were invoking it as a IIFE by adding () at the end of the function 您不是通过传递匿名函数,而是通过在函数末尾添加()来将其作为IIFE调用

function register() {
    $.post("../api/Register", {
        'Email': $("#rEmail").val(),
            'Password': $("#rPassword").val()
    }).done(function () {
        alert("Thank you for registering!");
    }).fail(function () {
        alert("Email already exists");
    });

}

The problem is you're immediately executing the functions that are getting passed to done and fail . 问题是您要立即执行传递给donefail That's causing these functions to be executed right then and there. 这导致这些功能在那时和那里立即执行。

So just pass the function itself by changing this 所以只需通过更改此函数本身来传递

.done((function () {
    alert("Thank you for registering!");
})())

to this 对此

.done(function () {
    alert("Thank you for registering!");
})

You really shouldn't be sending an http status of 500 on an expected registration failure such as "email already exists" condition... this should be handled by a parameter that denotes success / failure as part of a 200 response. 您确实不应该在预期的注册失败(例如“电子邮件已存在”)的情况下发送500的http状态……这应该由一个表示成功/失败的参数作为200响应的一部分来处理。

You can handle unexpected internal server errors (status 500) using success or error callbacks like so: 您可以使用成功或错误回调来处理意外的内部服务器错误(状态500),如下所示:

$.ajax({
    url : "../api/Register",
    type : "post",
    data : {"Email" : "you@example.com", "Password" : "pw"},
    dataType : "json",
    success : function(response){
        // handle http 200 responses
        if(response.registrationApproved){
            alert("Thank you for registering!");
        }else{
            alert("email exists");
        }
    },
    error : function(){
        // handle 500 or 404 responses
        alert("server call failed");
    },
    complete : function(){
        // if needed..  this will be called on both success and error http responses
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM