javascript - jQuery无法在标准promise中使用,求解
PHP中文网
PHP中文网 2017-04-11 09:56:35
[JavaScript讨论组]

使用最新的jQuery,不是说“1.5版本之后,$.ajax()的返回对象实现了CommonJS的Promises/A接口”了吗?


function getJSON(){
    var promise = new Promise(function(resolve,reject){
        jQuery.post('http://127.0.0.1').then(function(data){
            resolve(data)
        })
    })
    return promise
}

jQuery.post('http://127.0.0.1').then(function(){
    return Promise.all([sonthing])
}).then(function(result){
    console.log('result不正常')
})

getJSON().then(function(){
    return Promise.all([sonthing])
}).then(function(result){
    console.log('result正常')
})

希望大神解答,谢谢

PHP中文网
PHP中文网

认证0级讲师

全部回复(1)
PHPz

可能跟你使用的jquery版本有一定关系。

1.5之前返回的是xhr对象,省略。

1.5之后,返回的是deferred对象:

var promise=$.ajax({
    //
});
promise.done(function(){});
promise.fail(function(){});
promise.always(function(){});

1.8之后返回的才是promise对象,注意1.8之前的deferred对象是有then方法的,但不是promise对象。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号