简体   繁体   English

返回false不会停止ajax $ .post中的代码

[英]Return false doesn't stop the code in ajax $.post

I execute an ajax request using $.post , this is my code: 我使用$.post执行ajax请求,这是我的代码:

$.post(postUrl, postData, function(response)
{
      if(response.status == "SUCCESS")
      {
             updateConfirmFrame();
      }
      else
      {
             return false;
      }
 }, 'json');

now if the response return SUCCESS the code continue calling a function that unlock some control, but if an exception is handled, so the code should blocked. 现在,如果响应返回SUCCESS则代码将继续调用用于解锁某些控件的函数,但是如果处理了异常,则应阻止代码。 Now I've put an alert in return false; 现在,我将警报return false; condition and the alert is printed correctly but, the return false doesn't stop the code. 条件,并且警报已正确打印,但是,返回false不会停止代码。 I don't want that the code continue the execution. 我不希望代码继续执行。 I also tried with throw Exception but doesn't working. 我也尝试使用throw Exception但是不起作用。 What am I doing wrong? 我究竟做错了什么?

UPDATE 更新

$.post(postUrl, postData)

.done(function(response)
{
    if(response.status == "SUCCESS")
    {
        updateConfirmFrame();
    }
    else
    {
            alert("error"); //The problem is here
            return false;
     }
 })
 .fail(function(err)
 {
       return false;
 });

alert("hi wor")

Use that: 使用:

$.post(url, postdata)
 .done(function(response) {

  })
 .fail(function(err) {
   // error
 });

The syntax you used is $.post(postUrl, postData, success_callback, fail_callback); 您使用的语法是$.post(postUrl, postData, success_callback, fail_callback);

return statements only return the function they are within, stopping further code execution in that function . return语句仅返回它们所在的函数,从而停止该函数中的进一步代码执行。 They do not stop JS code execution in general. 它们通常不会停止JS代码的执行。

The alert statement you are saying shouldn't run because you've done return false; 您说的警报语句不应运行,因为完成后return false; is not in the function returning false. 在函数中不返回false。 It would not be affected by the fact that some other function returned false. 不会受到其他某些函数返回false的事实的影响。 It absolutely should run. 它绝对应该运行。


On top of that, the success function for your post call is an event callback. 最重要的是,您的帖子调用的成功功能是事件回调。 It is created, but does not run until the actual loading of the file happens, which is gonna be long after other code outside your ajax stuff finishes. 它是已创建的,但是直到文件的实际加载发生后才运行,这将在ajax之外的其他代码完成很长时间之后进行。 So the code in that function isn't even gonna be executing before that alert takes place. 因此,在该警报发生之前,该函数中的代码甚至都不会执行。

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

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