Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我需要返回值​;用于货币转换的函数中的变量_Javascript_Json_Ajax - Fatal编程技术网

Javascript 我需要返回值​;用于货币转换的函数中的变量

Javascript 我需要返回值​;用于货币转换的函数中的变量,javascript,json,ajax,Javascript,Json,Ajax,我需要返回值​​用于货币转换的函数中的变量。 我从乌克兰银行的网站上获得了三种货币的汇率信息 我已经从函数中接收到数据,但无法在其他函数中使用 我想做一个货币转换器,为了不再次发出请求,我想使用它们 我不知道在哪里写回信。而且,这个函数有一个函数,问题是,你能告诉我怎么处理它吗 有一个链接到该网站,但它是在乌克兰 var-URItwo=`https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?json`; const XHR=ne

我需要返回值​​用于货币转换的函数中的变量。 我从乌克兰银行的网站上获得了三种货币的汇率信息

我已经从函数中接收到数据,但无法在其他函数中使用 我想做一个货币转换器,为了不再次发出请求,我想使用它们

我不知道在哪里写回信。而且,这个函数有一个函数,问题是,你能告诉我怎么处理它吗

有一个链接到该网站,但它是在乌克兰
var-URItwo=`https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?json`;
const XHR=new XMLHttpRequest();
addEventListener(“加载”,checkrate(),false);
函数校验率(e){
addEventListener(“readystatechange”,function()){
如果((XHR.readyState==4)和&(XHR.status==200)){
var resulttwo=JSON.parse(XHR.responseText);
console.log(结果二);
for(结果2中的var键){
如果(resulttwo[key].cc==“USD”){
rateone.innerHTML=resulttwo[key].txt++`+resulttwo[key].rate.toFixed(2)+`ГППц`;
cursUSD=resultwo[key].rate.toFixed(2);
console.log(美元);
}
否则,如果(结果二[key].cc==“EUR”){
ratetwo.innerHTML=resultTwo[key].txt++`+resultTwo[key].rate.toFixed(2)+`ГППц`;
游标=resultwo[key].rate.toFixed(2);
控制台日志(cursEUR);
}
else if(resultwo[key].cc==“PLN”){
ratetree.innerHTML=resulttwo[key].txt++`+resulttwo[key].rate.toFixed(2)+`ΓППц`;
cursPLN=ResultTwo[key].rate.toFixed(2);
控制台日志(PLN);
}
}
}
},假);
XHR.open(“GET”,URItwo,true);
XHR.send();

}
为了避免再次发出请求,您可以在某些全局变量中预先设置响应,以便可以从所需的方法访问响应

函数fetchExchangeData(回调){ 警察二号=`https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?json`; const XHR=new XMLHttpRequest(); XHR.open(“GET”,URItwo,true); XHR.onreadystatechange=函数(){ 如果(XHR.readyState==4&&XHR.status==200){ var resulttwo=JSON.parse(XHR.responseText); console.log(结果二); 回调(resulttwo,null); }否则{ 回调(null,XHR.statusText);//返回错误 } } XHR.send(); } 函数操作(结果二,错误){ for(结果2中的var键){ 如果(resulttwo[key].cc==“USD”){ rateone.innerHTML=resulttwo[key].txt++`+resulttwo[key].rate.toFixed(2)+`ГППц`; cursUSD=resultwo[key].rate.toFixed(2); console.log(美元); } 否则,如果(结果二[key].cc==“EUR”){ ratetwo.innerHTML=resultTwo[key].txt++`+resultTwo[key].rate.toFixed(2)+`ГППц`; 游标=resultwo[key].rate.toFixed(2); 控制台日志(cursEUR); } else if(resultwo[key].cc==“PLN”){ ratetree.innerHTML=resulttwo[key].txt++`+resulttwo[key].rate.toFixed(2)+`ΓППц`; cursPLN=ResultTwo[key].rate.toFixed(2); 控制台日志(PLN); } } } 函数onLoad(){ //您可以使用ES6的异步和等待关键字(承诺)而不是回调。 FetchExchangeData(函数(数据){ //将数据存储在某个变量中,以便在其他函数中使用。 操纵(数据); ... 其他方法(数据); }); } window.onload=onload;
您能解释一下回调的含义和用途吗?和数据变量。一般来说,你可以解释这里的一切是如何运作的?我能在其他方法中使用这些(cursuUSD、cursur、cursPLN)变量吗?您可以在这里准备好回调-数据变量保存来自服务器的响应