async:false选项在$ .ajax()中不起作用,是否在所有用例中都在jQuery 1.8+中贬值了? [英] async:false option not working in $.ajax() , is it depreciated in jQuery 1.8+ for all use cases?

查看:375
本文介绍了async:false选项在$ .ajax()中不起作用,是否在所有用例中都在jQuery 1.8+中贬值了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在$ .ajax()中使用async:false选项感到困惑.根据$ .ajax()文档:

I'm confused about using the async: false option with $.ajax(). According to the $.ajax() documentation:

从jQuery 1.8开始,不建议使用async:false和jqXHR($ .Deferred); 您必须使用成功/错误/完成回调选项,而不是相应的 jqXHR对象的方法,例如jqXHR.done()或已弃用的jqXHR.success().

As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success().

我不知道jqXHR($ .Deferred)是什么意思.使用async:false是否出于某种原因而贬值,或者jqXHR($ .Deferred)是某种特殊的用例?

I don't know what jqXHR ($.Deferred) means. Is using async:false for any reason depreciated, or is jqXHR ($.Deferred) some sort of special use case?

我问是因为我在异步地进行$ .ajax()调用时遇到问题.这是jQuery 1.8.2:

I ask as I'm having trouble getting an $.ajax() call to happen asynchronously.This is with jQuery 1.8.2:

var ret = {};

$.ajax({
   async:           false,
    method:         'GET',
    contentType:    'application/json',
    dataType:       'jsonp',
    url:            '/couchDBserver',
    error:          myerr,
    success:        function(data) {

        var rows = data.rows;

        //something that takes a long time
        for(var row in rows) {
             ret[rows[row].key] = rows[row].value;
        }

        console.log('tick');
    }
});
console.log('tock');
console.log(JSON.stringify(ret))

我的控制台输出是:

t
{}
滴答

tock
{}
tick

我做错了什么,还是我做错了 ?

Am I doing something wrong, or am I doing something wrong?

推荐答案

您尝试同时将JSONP技术与async:false结合使用.这是不可能的. JSONP实际上是在创建script元素并将其附加到文档的某个位置,因此它不是XHR,jQuery无法在任何地方传递sync标志.由于您从同一来源获得数据,因此只需将dataType更改为

You trying to use JSONP techinque with async:false at the same time. This is not possible. JSONP is actually creating a script element and appending it somewhere to the document, so it's not an XHR and jQuery can't pass the sync flag anywhere. Since you get data from the same origin, just change dataType to

dataType:       'json',

但是,每个人都可以告诉您同步请求不好,它们会挂起您的浏览器.您只应在少数情况下使用它们.

However, everyone can tell you that synchronous requests are not good they hang your browser. You should use them only in a small number of cases.

这篇关于async:false选项在$ .ajax()中不起作用,是否在所有用例中都在jQuery 1.8+中贬值了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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