Ajax成功:{return false;} [英] Ajax success: {return false;}

查看:79
本文介绍了Ajax成功:{return false;}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

成功 $。ajax 返回 false $ c>已完成:

I want to return false from $.ajax when success is complete:

$.ajax({
    url: '' + $website_url + 'queries/voorraad_berekenen.php',
    type: 'post',
    data: {
        aantal: $(this).parent('form').children('.quantity').val(),
        item_number_1: $(this).parent('form').children('.item_number_1').val()
    },
    success: function(result) {
        return false;
    }
});

这不起作用。有没有解决方法?

This doesn't work. Is there a work around?

推荐答案

从你的帖子我想你调用的函数包含 $ .ajax()并尝试返回false 到该函数。但你不能这样做,因为 AJAX 是异步的。

From your post I guess that you call a function that contains the $.ajax() and try to return false to that function. but you can't do that such way, because AJAX is asynchronous.

最好从ajax成功函数调用函数,如下所示:

It's better to call a function from the ajax success function like following:

$.ajax({
    url: '' + $website_url + 'queries/voorraad_berekenen.php',
    type: 'post',
    data: {
        aantal: $(this).parent('form').children('.quantity').val(),
        item_number_1: $(this).parent('form').children('.item_number_1').val()
    },
    success: function(result) {
        var returned = true;
        if(some_condition_not_satisfied) {
          returned = false;
        } else {

        }
        // call a function
        calledFromAjaxSuccess(returned);
    }
});

function calledFromAjaxSuccess(result) {
  if(result) {
    alert('TRUE');
  } else {
    alert('FALSE');
  }
}

这篇关于Ajax成功:{return false;}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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