你好代碼嚮導,如何在JavaScript中返回通過多鏈接功能的對象
我想從樓下指定的網址,以獲取JSON數據和在中的GetWeather之外的陣營組件使用它()函數。從內到外傳遞一個對象的有效方法是什麼?我的控制檯讀取未定義。我相信有一個變量範圍的問題......感謝一個想法來解決它。
下面是代碼
function getWeather() {
var url = 'http://api.openweathermap.org/data/2.5/weather?zip=3000,au';
var apiKey= "005fa98ae858a29acf836ecdefac0411";
var httpRequest;
makeRequest();
var response;
function makeRequest() {
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = responseMethod;
httpRequest.open('GET', url + '&appid=' + apiKey);
httpRequest.send();
}
function responseMethod() {
if(httpRequest.readyState === 4) {
if(httpRequest.status === 200) {
updateUISucess(httpRequest.responseText);
} else {
}
}
}
function updateUISucess(responseText) {
return response = JSON.parse(responseText);
// I would like to get this JSON object out so that it logs on the console.
}
return response;
};
console.log(getWeather());
爲什麼不能你可以的GetWeather和它的狀態存儲在什麼反應呢? –
這就是我想要做的。但是,我不知道如何首先獲取數據並將其傳遞給getInitialState。 – Hooey
既然你是一個異步調用,我會嘗試'react-promise'。 https://www.npmjs.com/package/react-promise –