我正在使用英特爾XDK製作轉換工具,我的腳本檢測設備是否具有互聯網連接,如果是使用此功能從openexchangerates通過JSON獲取最新費率並將其存儲在localstorage中:Money.js + LocalStorage費率
$.getJSON('https://openexchangerates.org/api/latest.json?app_id=XXXXXXXXXXXXXXXXXXXXXXXXXX',function(data) {
var localData = JSON.stringify(data);
localStorage.setItem('convrates', localData);
// Check money.js has finished loading:
if (typeof fx !== "undefined" && fx.rates) {
fx.rates = data.rates;
fx.base = data.base;
} else {
// If not, apply to fxSetup global:
var fxSetup = {
rates : data.rates,
base : data.base
}
}
});
這沒關係!但是,當試圖從localStorage的這個信息,什麼都不會發生......
var localData = JSON.parse(localStorage.getItem('convrates'));
$.getJSON(localData,function(data) {
// Check money.js has finished loading:
if (typeof fx !== "undefined" && fx.rates) {
fx.rates = data.rates;
fx.base = data.base;
} else {
// If not, apply to fxSetup global:
var fxSetup = {
rates : data.rates,
base : data.base
}
}
});
這是JSON數據爲例:
{
"disclaimer": "Exchange rates are provided for informational purposes only, and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, NO guarantees are given whatsoever of accuracy, validity, availability, or fitness for any purpose - please use at your own risk. All usage is subject to your acceptance of the Terms and Conditions of Service, available at: https://openexchangerates.org/terms/",
"license": "Data sourced from various providers with public-facing APIs; copyright may apply; resale is prohibited; no warranties given of any kind. Bitcoin data provided by http://coindesk.com. All usage is subject to your acceptance of the License Agreement available at: https://openexchangerates.org/license/",
"timestamp": 1417507252,
"base": "USD",
"rates": {
"AED": 3.673268,
"AFN": 57.871426,
"ALL": 112.5408,
"AMD": 439.297503,
"ANG": 1.7891,
"AOA": 101.106125,
"ARS": 8.531535,
"AUD": 1.174523,
"AWG": 1.79,
"AZN": 0.783933,
"BAM": 1.570651,
"BBD": 2,
"BDT": 77.73667,
}
}
我真的不知道如何做到這一點。