2017-06-15 37 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); 
 
}