xhr promise返回错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xhr promise返回错误相关的知识,希望对你有一定的参考价值。

我想在其他函数中显示XMLhttpRequest的答案(不在onreadystatechangefunction中)。

我有代码

  setInterval(function() {
    let requestPromise = new Promise((resolve, reject) => {
        var xhr = new XMLHttpRequest();
            xhr.open('POST', "location?act=update&id=<?php echo $id; ?>", true);
            xhr.send();
            xhr.onreadystatechange = function(){
                if(this.readyState==4){
                    if(this.status == 200){
                        if(this.responseText){
                            try{
                                var data = JSON.parse(this.responseText);
                                if(data.command=="list"){
                                    var answerLat = parseFloat(data.lat);
                                    var answerLng = parseFloat(data.lng);
                                    var answerAccuracy = String(data.accuracy);
                                    var answerTime = String(data.time);
                                    resolve(answerLat); // Pump answerLat
                                    resolve(answerLng); 
                                    resolve(answerAccuracy);
                                    resolve(answerTime); 
                                }
                                }catch(e){
                            }
                        }
                    }
                }
            }
    });
    requestPromise.then(googleMapsFunction); // googleMapsFunction will be called once the request is completed and will get answerLat as the argument
}, 20000);

例如:我在其他功能中有谷歌地图。我如何从xmlhttpreqest加载数据(answerLat,answerLng,answerAccuracy和answerTime)到map(function - googleMapsFunction)的其他函数?

但是当我使用它时,我看到错误:

 Uncaught (in promise) ReferenceError: answerLat is not defined

我如何接受它作为参数?

答案

我想问题在于解决承诺。尝试将收到的数据合并到单个对象中并将其传递给resolve()

setInterval(function() {
    let requestPromise = new Promise((resolve, reject) => {
        var xhr = new XMLHttpRequest();
        xhr.open('POST', "location?act=update&id=<?php echo $id; ?>", true);
        xhr.send();
        xhr.onreadystatechange = function(){
            if(this.readyState==4 && this.status == 200 && this.responseText){
                try{
                    var data = JSON.parse(this.responseText);
                    if(data.command=="list"){
                        resolve({
                            answerLat: parseFloat(data.lat),
                            answerLng: parseFloat(data.lng),
                            answerAccuracy: data.accuracy,
                            answerTime: data.time
                        });
                    }
                }catch(e){
                    reject(e)
                }
            }
        }
    });
    requestPromise.then(googleMapsFunction); // googleMapsFunction will be called once the request is completed and will get answerLat as the argument
}, 20000);

const googleMapsFunction = (params) => {
    const {answerLat, answerLng, answerAccuracy, answerTime} = params
    // ... answerLat, answerLng, answerAccuracy, answerTime
} 

以上是关于xhr promise返回错误的主要内容,如果未能解决你的问题,请参考以下文章

中止 DoJo XHR 请求

使用Promise.race实现超时机制取消XHR请求

VSCode自定义代码片段12——JavaScript的Promise对象

VSCode自定义代码片段12——JavaScript的Promise对象

javascript XHR_with_promise

我如何宣传原生XHR?