简体   繁体   English

ajax vs请求openWeatherMap

[英]ajax vs request openWeatherMap

so i need to draw a icons on map when status is ok. 所以当状态确定时,我需要在地图上绘制图标。 But when I change request to ajax i got 但是当我将请求更改为ajax时我得到了

Unexpected token u in JSON at position 0 JSON中位置0处的意外令牌u

when i go back to request everything work fine... but I need to have exceptions handling, so thats why Ajax 当我回去要求一切正常...但是我需要进行异常处理,所以这就是为什么Ajax

JS JS

    function displayXML()
{
    if (this.readyState == 4)
    { 
        proccessResults();

        if (this.status == 200)
        {
            console.log(this.responseXML);

        } else {
            console.log(this.status);
            alert('Something went wrong')
        }
    }
}
var getWeather = function(northLat, eastLng, southLat, westLng) {
    gettingData = true;
    var requestString = "http://api.openweathermap.org/data/2.5/box/city?bbox="
        + westLng + "," + northLat + ","
        + eastLng + "," + southLat + ","
        + map.getZoom()
        + "&cluster=yes&format=json"
        + "&APPID=" + openWeatherMapKey;
    var ajax = new XMLHttpRequest();
    ajax.open('GET', requestString, true);
    ajax.onreadystatechange = displayXML;
    ajax.send();
    // request = new XMLHttpRequest();
    // request.onload = proccessResults;
    // request.open("get", requestString, true);
    // request.send();
};

var proccessResults = function() {

    var results = JSON.parse(this.responseText);
    if (results.list.length > 0) {
        resetData();
        for (var i = 0; i < results.list.length; i++) {
            geoJSON.features.push(jsonToGeoJson(results.list[i]));
        }
        drawIcons(geoJSON);
    }
};

respone: 回应:

{"cod":"200","calctime":0.0007,"cnt":2,"list":[{"id":3082473,"name":"Wejherowo","coord":{"lon":18.23559,"lat":54.605679},"main":{"temp":15.33,"pressure":1012,"humidity":93,"temp_min":15,"temp_max":15.56},"dt":1468456804,"wind":{"speed":0.5,"deg":0},"clouds":{"all":20},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}]},{"id":3099424,"name":"Gdynia","coord":{"lon":18.531879,"lat":54.51889},"main":{"temp":15.3,"pressure":1012,"humidity":93,"temp_min":15,"temp_max":15.56},"dt":1468456816,"wind":{"speed":0.5,"deg":0},"clouds":{"all":20},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}]}]}

Your proccessResults function is not being called with any parameters. 您的proccessResults函数未使用任何参数调用。 It has no idea what this (or this.responseText ) is. 它不知道this (或this.responseText )是什么。 You are trying to parse the string "undefined" as JSON. 您正在尝试将字符串“ undefined”解析为JSON。 That's where your "Unexpected token u" error comes from. 这就是您的“意外令牌u”错误的来源。

Try to change your processResults function to take a parameter : 尝试将您的processResults函数更改为采用参数

var proccessResults = function(jsonString) {    
    var results = JSON.parse(jsonString);
    if (results.list.length > 0) {
        resetData();
        for (var i = 0; i < results.list.length; i++) {
            geoJSON.features.push(jsonToGeoJson(results.list[i]));
        }
        drawIcons(geoJSON);
    }
}

Then when you call it, pass it the this.responseText : 然后,当您调用它时,将其传递给this.responseText

function displayXML(){
    if (this.readyState == 4){ 
        proccessResults(this.responseText);

        if (this.status == 200){
            console.log(this.responseXML);

        }
        else {
            console.log(this.status);
            alert('Something went wrong')
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM