xhr promise返回错误

时间:2017-12-25 19:48:46

标签: javascript xmlhttprequest

我想在其他函数中显示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(函数 - googleMapsFunction)的其他函数?

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

 Uncaught (in promise) ReferenceError: answerLat is not defined

我如何接受它作为参数?

1 个答案:

答案 0 :(得分:1)

我想问题在于解决承诺问题。尝试将收到的数据合并到单个对象中并将其传递给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
}