等到jquery ajax请求完成并返回值

时间:2021-09-26 19:43:43

I want to wait until ajax call complete and return the value.

我想等到ajax调用完成并返回值。

function isFileExists(url) {
    var res = false;
    $.ajax({
        type: "GET",
        async: false,
        url: url,
        crossDomain: true,
        dataType: "script",
        success: function () {
            res = true;
        },
        error: function () {
            res = error;
        },
        complete: function () {

        }
    });
    return res; //It is always return false
}

I want to return the value "true/error"

我想返回值“true / error”

Please help me.

请帮帮我。

2 个解决方案

#1


10  

You can't do this. That's not how ajax works. You can't depend on the ajax request completing at any given time ... or ever completing. Any work you need to do that is based on the ajax request must be done in the ajax callbacks.

你不能这样做。这不是ajax的工作方式。您不能依赖于在任何给定时间完成的ajax请求......或者永远完成。您需要做的任何基于ajax请求的工作必须在ajax回调中完成。

jQuery makes it easy to bind callbacks (because jqXHR returned by jQuery's ajax methods implements Deferred):

jQuery使绑定回调变得容易(因为jQuery的ajax方法返回的jqXHR实现了Deferred):

var jqXHR = $.ajax({/* snip */});

/* millions of lines of code */

jqXHR.done(function () {
    console.log('true');
}).fail(function () {
    console.log('false');
});

P.S. you can do what you want to do if you set async to false, but that locks up the browser while the request runs. Don't do it. Then you just have jax.

附:如果将async设置为false,则可以执行您想要执行的操作,但在请求运行时会锁定浏览器。不要这样做。那你就是jax。

EDIT: You can't combine crossDomain: true and async: false. Cross Domain must be asynchronous.

编辑:您不能组合crossDomain:true和async:false。跨域必须是异步的。

#2


0  

maybe this will work for you:

也许这对你有用:

function isFileExists(url, init) {
    var res = null;
    var _init = init || false;

    if (!_init) {
        _init = true;
        $.ajax({
             type: "GET",
             url: url,
             crossDomain: true,
             dataType: "script",
             success: function () {
                 res = true;
             },
             error: function () {
                 res = 'error';
             },
             complete: function () {

             }
        });
    }

    if (res==null) {
        setTimeout(function(){ isFileExists(url, _init); }, 100);
    } else {
        return res;
    }
}

I tested it briefly, but not cross domain.

我简要测试了一下,但没有跨域测试。

#1


10  

You can't do this. That's not how ajax works. You can't depend on the ajax request completing at any given time ... or ever completing. Any work you need to do that is based on the ajax request must be done in the ajax callbacks.

你不能这样做。这不是ajax的工作方式。您不能依赖于在任何给定时间完成的ajax请求......或者永远完成。您需要做的任何基于ajax请求的工作必须在ajax回调中完成。

jQuery makes it easy to bind callbacks (because jqXHR returned by jQuery's ajax methods implements Deferred):

jQuery使绑定回调变得容易(因为jQuery的ajax方法返回的jqXHR实现了Deferred):

var jqXHR = $.ajax({/* snip */});

/* millions of lines of code */

jqXHR.done(function () {
    console.log('true');
}).fail(function () {
    console.log('false');
});

P.S. you can do what you want to do if you set async to false, but that locks up the browser while the request runs. Don't do it. Then you just have jax.

附:如果将async设置为false,则可以执行您想要执行的操作,但在请求运行时会锁定浏览器。不要这样做。那你就是jax。

EDIT: You can't combine crossDomain: true and async: false. Cross Domain must be asynchronous.

编辑:您不能组合crossDomain:true和async:false。跨域必须是异步的。

#2


0  

maybe this will work for you:

也许这对你有用:

function isFileExists(url, init) {
    var res = null;
    var _init = init || false;

    if (!_init) {
        _init = true;
        $.ajax({
             type: "GET",
             url: url,
             crossDomain: true,
             dataType: "script",
             success: function () {
                 res = true;
             },
             error: function () {
                 res = 'error';
             },
             complete: function () {

             }
        });
    }

    if (res==null) {
        setTimeout(function(){ isFileExists(url, _init); }, 100);
    } else {
        return res;
    }
}

I tested it briefly, but not cross domain.

我简要测试了一下,但没有跨域测试。