所以我需要在地图上画一个图标,当状态正常的时候。但是当我将请求改为ajax时,我得到了
位置0处JSON中的意外令牌u
当我回去要求的时候一切正常..。但是我需要异常处理,所以这就是Ajax的原因。
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);
}
};答复:
{"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"}]}]}发布于 2016-07-19 14:40:12
不使用任何参数调用proccessResults函数。它不知道this (或this.responseText)是什么。您正在尝试将字符串"undefined“解析为JSON。这就是你的“意外标记u”错误的来源。
尝试将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);
}
}然后当你调用它时,把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')
}
}
}https://stackoverflow.com/questions/38460847
复制相似问题