jQuery函数不返回值

时间:2018-09-10 14:21:45

标签: jquery function asynchronous return

问题很大,但解决方法很简单。

我的旧功能正常运行,必须返回true或false。

function char_existz(char_name, server) {
    $.ajaxSetup({async: false});
    $.getJSON("/index.php?f=payment&char_name=" + char_name + "&s=" + server, function (json) {
        res = json.k;
    });
    $.ajaxSetup({async: true});
    console.log(res === 1);

    return (res === 1);
}

在使用它时,我得到警告:[弃用]不赞成在主线程上使用同步XMLHttpRequest,因为它对最终用户的体验有不利影响。

我想在没有警告的情况下尝试使用类似这样的东西在$ .ajax上尝试

function char_existzz(char_name, server) {
    $.ajax({
        type: "POST",
        url: '/payment',
        data: {char_name: char_name, s: server},
        success: function (data) {
            result = (data.k===1);
        },
        dataType: 'json'
    });
    return result;
}
ce = char_existz(character.val(), server.val());

                if (ce === false) {
                    alert('Player not found!');
                    return false;
                }

但是它不会在变量中返回true或false,所以我不知道怎么做,因为在旧功能上,当我不触摸 async

时,它是有效的

0 个答案:

没有答案
相关问题