Javascript Promise resolve上JSON输入意外结束
我从OpenWeatherAPI请求天气数据,每次我的承诺得到解决并执行JSON.pase(daat)时,我都会得到“error SyntaxError:Object.parse()处JSON输入的意外结束” 我得到的JSON如下所示:Javascript Promise resolve上JSON输入意外结束,javascript,json,http,reactjs,es6-promise,Javascript,Json,Http,Reactjs,Es6 Promise,我从OpenWeatherAPI请求天气数据,每次我的承诺得到解决并执行JSON.pase(daat)时,我都会得到“error SyntaxError:Object.parse()处JSON输入的意外结束” 我得到的JSON如下所示: { "coord":{"lon":24.94,"lat":60.17}, "weather":[{"id":741,"main":"Fog","description":"fog","icon":"50n"}], "base":"stations", "
{
"coord":{"lon":24.94,"lat":60.17},
"weather":[{"id":741,"main":"Fog","description":"fog","icon":"50n"}],
"base":"stations",
"main":{"temp":273.15,"pressure":994,"humidity":94,"temp_min":273.15,"temp_max":273.15},
"visibility":500,
"wind":{"speed":1.5},
"clouds":{"all":90},
"dt":1489872000,
"sys":{"type":1,"id":5019,"message":0.4444,"country":"FI","sunrise":1489811101,"sunset":1489854738},
"id":658225,
"name":"Helsinki",
"cod":200
}
httpRequestWeather(weatherNowHTTPQuery + "Helsinki" + weatherAPIToken).then(function(data){
let myData = JSON.parse(data);
//always returns error when trying to do JSON.parse()
self.setState({
weatherConditions: "Weather is here!"
});
self.setState({
//always returns error when trying to do JSON.parse()
weatherObj: JSON.parse(data)
});
}).catch(function(err){
console.log("error", err);
self.setState({
weatherConditions: "Buy"
});
它似乎是完全有效的,至少从普通的JS文件中请求它。
尽管来自我的React应用程序(请参阅问题:)
我的代码如下所示:
{
"coord":{"lon":24.94,"lat":60.17},
"weather":[{"id":741,"main":"Fog","description":"fog","icon":"50n"}],
"base":"stations",
"main":{"temp":273.15,"pressure":994,"humidity":94,"temp_min":273.15,"temp_max":273.15},
"visibility":500,
"wind":{"speed":1.5},
"clouds":{"all":90},
"dt":1489872000,
"sys":{"type":1,"id":5019,"message":0.4444,"country":"FI","sunrise":1489811101,"sunset":1489854738},
"id":658225,
"name":"Helsinki",
"cod":200
}
httpRequestWeather(weatherNowHTTPQuery + "Helsinki" + weatherAPIToken).then(function(data){
let myData = JSON.parse(data);
//always returns error when trying to do JSON.parse()
self.setState({
weatherConditions: "Weather is here!"
});
self.setState({
//always returns error when trying to do JSON.parse()
weatherObj: JSON.parse(data)
});
}).catch(function(err){
console.log("error", err);
self.setState({
weatherConditions: "Buy"
});
但是,在http请求内部,数据的解析非常好:
function httpRequestWeather(url) {
return new Promise(function(resolve, reject) {
var weatherRequest = new XMLHttpRequest();
weatherRequest.open("GET", url, true);
weatherRequest.onreadystatechange = function() {
if ( this.status === 200) {
console.log("request finished and response is ready");
console.log(this.response);
//this works perfectly fine
self.setState({
weatherConditions: this.response
});
resolve(this.response);
} else{
reject(new Error("no weather data"));
}
};
weatherRequest.send();
});
}
您没有检查
readyState
<代码>状态在readyState
为4之前可以是200
。请记住,您会收到对onreadystatechange
处理程序的多个回调,但承诺只能解决一次
因此,改变:
if ( this.status === 200) {
// ...success...
} else {
// ...error...
}
到
以下是您在JSFIDLE上的原始版本(稍作修改以使用JSFIDLE的JSON echo内容),显示了错误:
下面是一个上面的更新,显示它正在工作:您是否尝试了
weatherObj:myData
替代?不工作,仍然存在错误SyntaxError:在Weather.js:45的Object.parse()处JSON输入意外结束浏览器中的URL API调用是否正确呈现JSON响应?数据已经被解析,只需设置weatherObj:data
而不是解析它。@RandomUser我想你是对的。谢谢!这有帮助!