2017-06-15 41 views
0

你好代碼嚮導,如何在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()); 
+1

爲什麼不能你可以的GetWeather和它的狀態存儲在什麼反應呢? –

+0

這就是我想要做的。但是,我不知道如何首先獲取數據並將其傳遞給getInitialState。 – Hooey

+0

既然你是一個異步調用,我會嘗試'react-promise'。 https://www.npmjs.com/package/react-promise –

回答

0

瀏覽器,現在允許返回你的功能,u需要同步請求。請嘗試節點webkit。

+0

我確實現在使用Chrome。我如何嘗試節點webkit? – Hooey

+0

我推薦這個版本,適合編程。 https://dl.nwjs.io/v0.12.2/nwjs-v0.12.2-win-ia32.zip – genife

+0

而這裏的官方網頁有當前的穩定版本,https://nwjs.io/ – genife

0

您可以使用回調方法模式來獲取數據,並使用它以外

//Get weather function which accepts a callback function as a parameter 
 
function getWeather(callback) { 
 
    var url = 'http://api.openweathermap.org/data/2.5/weather?zip=3000,au'; 
 
    var apiKey = "005fa98ae858a29acf836ecdefac0411"; 
 
    var httpRequest; 
 
    makeRequest(); 
 

 

 
    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) { 
 
     try { 
 
      //Parse response 
 
      var data = JSON.parse(httpRequest.responseText); 
 
      //Invoke callback function with first arg null and second with result 
 
      callback(null, data); 
 
     } catch (e) { 
 
      //JSON parse failed 
 
      //Invoke callback with first arg as error object 
 
      callback(e) 
 
     } 
 
     } else { 
 
     
 
     //Invoke callback with first arg as error object 
 
     callback({ 
 
      success: false, 
 
      status: httpRequest.status 
 
     }); 
 
     } 
 
    } 
 
    } 
 
};

function Weather(props) { 
 
    return <p>{JSON.stringify(props.result)}</p>; 
 
} 
 

 
//Callback which accepts err and result 
 
function handleResult(err, result) { 
 
    if (err) { 
 
    //Check for errors and return early if needed 
 
    console.log("Error!", JSON.stringify(err)); 
 
    return; 
 
    } 
 
    //Assign to react state here 
 
    console.log(result); 
 
    const element = <Weather result ={result}/ > ; 
 
    ReactDOM.render(
 
    element 
 
); 
 

 
} 
 

 
//Invoke getWeather with callback function as parameter 
 
getWeather(handleResult);

+0

你能給我一個關於getWeather函數括號內的回調函數的例子嗎?謝謝! – Hooey

+0

已經提供了一個例子。向下滾動到結尾..(代碼片段窗格也滾動) –

+0

對不起,我看了一下最後一段代碼。我還不能理解你的第一行是什麼意思:function getWeather(callback)。我是否將回調函數作爲參數傳遞? – Hooey

0

由於阿賈克斯(異步)處理JS運行不斷按線路。
只是做一個回調函數,並將其傳遞給調用函數。

function getWeather(cb) { 
 
    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. 
 
    cb(response); 
 
    } 
 

 
    return response; 
 
}; 
 

 

 
getWeather(cb); 
 

 
function cb(res){ 
 
    alert(res); 
 
    console.log(res); 
 
}