jQuery AJAX函数返回true或false,仅返回false,而其一切正常 [英] jQuery AJAX function return true or false returning only false while its all good

查看:317
本文介绍了jQuery AJAX函数返回true或false,仅返回false,而其一切正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery函数返回true或false,在一切正常的情况下仅返回false

jQuery function return true or false, returning only false while it's all good

我不知道如何解决它和什么错误.

I don't know how to fix it and what wrong.

到目前为止,我已经知道了,代码:

So far i got to this , the code :

onclick函数:

the onclick function:

$(document).on('click', '.founditems', function (e) {
    var $this = $(this)
    var k = $this.attr('href');
    if (checkUserSystem(k) == true) {
        alert("good");

    } else {
        alert("bad");
    }
});

ajax函数:

function checkUserSystem(k) {
    var starttime = $.trim($('#checkusersystemstartime').val());
    var k = k;
    var dataString = 'k=' + k + '&starttime=' + starttime;
    $.ajax({
        type: "POST",
        url: "r.php?proc=sendCp",
        data: dataString,
        dataType: 'json',
        beforeSend: function () {

        },
        complete: function () {

        },
        success: function (data) {

            if (data.message == false) {

                return false;
            } else if (data.cblocked == true) {

                return false;
            } else {

                return true;
            }
        }
    });
    return false;
}

当函数正常工作并且PHP返回true时,该函数始终返回false, 如果我删除,则返回false;在ajax代码的末尾,它将返回undefined,而不是真正的主意吗?

The function always return false when it's working perfect and the PHP return true, And if I remove return false; at the end of the ajax code, it will return undefined instead of true any idea?

谢谢.

推荐答案

您将必须阅读异步执行内容.当ajax调用完成"时,将运行成功块,但是在$ .ajax()调用之后,代码将立即继续执行.

You will have to read up on async execution. The success block is run when the ajax call is 'complete, but the execution of your code will continue right away after the $.ajax() call.

发生未定义错误的原因是,除非您调用"return false"调用,否则该方法不会返回任何内容.

The reason for the undefined error is that the method doesn't return anything unless you ad the 'return false' call.

您可以将警报真/假部分作为功能参数,否则您将不得不看一下 在ajax调用的同步版本中.

Either you can take the alert true/false part as a function parameter or you will have to look at the synchronous version of ajax calls.

这篇关于jQuery AJAX函数返回true或false,仅返回false,而其一切正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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