Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
等待jQueryAjax请求完成并返回值_Jquery_Ajax_Return Value_File Exists - Fatal编程技术网

等待jQueryAjax请求完成并返回值

等待jQueryAjax请求完成并返回值,jquery,ajax,return-value,file-exists,Jquery,Ajax,Return Value,File Exists,我想等到ajax调用完成并返回值 function isFileExists(url) { var res = false; $.ajax({ type: "GET", async: false, url: url, crossDomain: true, dataType: "script", success: function () { res = true;

我想等到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
}
我想返回值“true/error”


请帮帮我。

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

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


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


编辑:不能将
跨域:true
异步:false
组合在一起。跨域必须是异步的。

也许这对您有用:

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;
    }
}

我对它进行了简短的测试,但没有跨域测试。

试试这段代码。这对我有用

 function getInvoiceID(url, invoiceId) {
        return $.ajax({
            type: 'POST',
            url: url,
            data: { invoiceId: invoiceId },
            async: false,
        });
    }
    function isInvoiceIdExists(url, invoiceId) {
        $.when(getInvoiceID(url, invoiceId)).done(function (data) {
            if (!data) {

            }
        });
    }

下一次考虑使用jQuery标签代替JavaScript,它会使它更具体,节省人们的时间。谢谢你的意思是
async
false
@ExplosionPills:我正在从另一个函数调用isFileExists(url)。我怎么打电话jqXHR@Sagar在
isFileExists
do
return$.ajax(…)
。然后您可以像使用
isFileExists(url).done(函数(){
@ExplosionPills isFileExists(url).done(函数(){//这里我想要结果值,我如何获得它});@Sagar是从服务器发送的结果值?然后只需执行
.done(函数(resultValue)){
是,在解决ajax请求之前应该调用它。尝试将“超时”设置添加到ajax调用中,将其设置为1分钟(可能),作为转义的手段。
 function getInvoiceID(url, invoiceId) {
        return $.ajax({
            type: 'POST',
            url: url,
            data: { invoiceId: invoiceId },
            async: false,
        });
    }
    function isInvoiceIdExists(url, invoiceId) {
        $.when(getInvoiceID(url, invoiceId)).done(function (data) {
            if (!data) {

            }
        });
    }