JQuery的阿贾克斯()的成功总是射击(即使服务器返回的错误code) [英] JQuery .ajax() success always firing (even when server returns error code)

查看:81
本文介绍了JQuery的阿贾克斯()的成功总是射击(即使服务器返回的错误code)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对将要被序列化的jQuery,并通过阿贾克斯()到URL发布的形式。

I have a form that is being serialized by JQuery and posted via .ajax() to a url.

的问题是,'成功:'函数总是叫,无论服务器是否返回一个错误,code与否。

The problem is that the 'success:' function is always called, regardless of whether the server returns an error code or not.

事实上,成功触发之前服务器响应(我已经把断点的服务请求的服务器的方法 - 这种方法完成​​之前成功触发)。如果服务器返回一个错误,code(如状态code 500)的JQuery调用成功和错误事件!

In fact, success fires even before the server has responded (I have put a breakpoint on the server method that services the request - success fires even before this method is completed). If the server returns an error code (e.g. Status code 500) JQuery calls BOTH success and error events!

任何想法是怎么回事?这是我的jQuery code:

Any ideas what's going on? Here's my jquery code:

$("#a-dialog").dialog({
    autoOpen: false,
    height: 300,
    width: 400,
    modal: true,
    buttons: {
    "Submit": function() {
                $.ajax({
                  type: 'POST',
                  url: theURL,
                  data: $("#a-dialog-form").serialize(),
                  success: alert('ok!') // THIS IS ALWAYS CALLED (IMMEDIATELY)
                });
        },
    },
});

更新:

这是我的一部分,一个愚蠢的错误!感谢blue112快速指出来:)

This was a stupid error on my part! Thanks to blue112 for quickly pointing it out :)

推荐答案

这是正常的,你必须把它作为一个回调,如:

That's normal, you must pass it as a callback, eg

            $.ajax({
              type: 'POST',
              url: theURL,
              data: $("#a-dialog-form").serialize(),
              success: function(){alert('ok!');} // The function will be called upon success.
            });

这篇关于JQuery的阿贾克斯()的成功总是射击(即使服务器返回的错误code)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆