我应该把 json 结果放在 money.js 的什么地方?
Where should I put the json result in money.js?
阅读文档http://openexchangerates.github.io/money.js/#fx.rates
它说你需要设置你的费率:
fx.base = "USD";
fx.rates = {
"EUR" : 0.745101, // eg. 1 USD === 0.745101 EUR
"GBP" : 0.647710, // etc...
"HKD" : 7.781919,
"USD" : 1, // always include the base rate (1:1)
/* etc */
}
我完全明白,只有这些才是静态汇率。它说要获得动态汇率,您需要添加 json api:
// Load exchange rates data via AJAX:
$.getJSON(
// NB: using Open Exchange Rates here, but you can use any source!
'http://openexchangerates.org/api/latest.json?app_id=[I hid this number]', 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
}
}
});
但是当我这样做时,欧元汇率没有改变,仍然是 0.74。费率不会改变或调整。
我应该把 json 请求放在哪里,在 money.js 脚本之前还是之后?
或者在 money.js 文件中?如果在 money.js 文件中,在底部还是顶部? - 或者请告诉我哪里出错了
一旦 money.js 加载后,您应该可以在任何地方重载它(javascript 通常允许这样做)。
没有足够的细节来明确说明,但我猜这是一个常见的竞争条件,因为网络调用是异步的,所以如果你正在做这样的事情:
-Load Money js
-Call Web call for rates
-Use money.js
很可能当您使用 money.js 时,您的费率调用尚未返回,因此当您调用它时您使用的是默认值。如果这是您的问题,您需要在实际设置费率时将代码放入回调中,如下所示:
$.getJSON(
// NB: using Open Exchange Rates here, but you can use any source!
'http://openexchangerates.org/api/latest.json?app_id=[I hid this number]', 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
}
}
// YOUR CODE HERE
});
文档实际上提到了这一点:
You'll need to wait until the AJAX request has completed before you can
begin processing conversions. You may also wish to cache
proximate/historical rates on your server and bootstrap them inline
into the HTML as a backup.
https://openexchangerates.org/documentation#example-javascript-ajax-jquery
JavaScript (AJAX / jQuery)
You can easily load the rates into an application or website with JavaScript using an AJAX request. I recommend using jQuery because it'll save you a tonne of headaches, and statistically speaking, you're probably already using it in your page/app:
// Use jQuery.ajax to get the latest exchange rates, with JSONP:
$.ajax({
url: 'http://openexchangerates.org/api/latest.json?app_id=YOUR_APP_ID',
dataType: 'jsonp',
success: function(json) {
// Rates are in `json.rates`
// Base currency (USD) is `json.base`
// UNIX Timestamp when rates were collected is in `json.timestamp`
// If you're using money.js, do this:
fx.rates = json.rates;
fx.base = json.base;
}
});
Use of JSONP is optional - jQuery will append a callback parameter to the URL, and the response will be wrapped in a function call. This is to prevent access-control (CORS) issues and will save you some headaches in many cases, though for security it's not tip-top (actually, if security is a primary concern in your app, you should proxy the results on your own server to prevent 100% against XSS attacks. Open Exchange Rates will support HTTPS soon, too.
The success callback is asynchronous - meaning that if you have code to be run straight away, which relies on the exchange rates being available, this code should be inside the callback. The rest of your program will keep on executing while that AJAX request is waiting.
强调一下: 如果您有代码可以立即 运行,这取决于可用的汇率,此代码应该是在回调中
阅读文档http://openexchangerates.github.io/money.js/#fx.rates 它说你需要设置你的费率:
fx.base = "USD";
fx.rates = {
"EUR" : 0.745101, // eg. 1 USD === 0.745101 EUR
"GBP" : 0.647710, // etc...
"HKD" : 7.781919,
"USD" : 1, // always include the base rate (1:1)
/* etc */
}
我完全明白,只有这些才是静态汇率。它说要获得动态汇率,您需要添加 json api:
// Load exchange rates data via AJAX:
$.getJSON(
// NB: using Open Exchange Rates here, but you can use any source!
'http://openexchangerates.org/api/latest.json?app_id=[I hid this number]', 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
}
}
});
但是当我这样做时,欧元汇率没有改变,仍然是 0.74。费率不会改变或调整。
我应该把 json 请求放在哪里,在 money.js 脚本之前还是之后? 或者在 money.js 文件中?如果在 money.js 文件中,在底部还是顶部? - 或者请告诉我哪里出错了
一旦 money.js 加载后,您应该可以在任何地方重载它(javascript 通常允许这样做)。
没有足够的细节来明确说明,但我猜这是一个常见的竞争条件,因为网络调用是异步的,所以如果你正在做这样的事情:
-Load Money js
-Call Web call for rates
-Use money.js
很可能当您使用 money.js 时,您的费率调用尚未返回,因此当您调用它时您使用的是默认值。如果这是您的问题,您需要在实际设置费率时将代码放入回调中,如下所示:
$.getJSON(
// NB: using Open Exchange Rates here, but you can use any source!
'http://openexchangerates.org/api/latest.json?app_id=[I hid this number]', 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
}
}
// YOUR CODE HERE
});
文档实际上提到了这一点:
You'll need to wait until the AJAX request has completed before you can
begin processing conversions. You may also wish to cache
proximate/historical rates on your server and bootstrap them inline
into the HTML as a backup.
https://openexchangerates.org/documentation#example-javascript-ajax-jquery
JavaScript (AJAX / jQuery)
You can easily load the rates into an application or website with JavaScript using an AJAX request. I recommend using jQuery because it'll save you a tonne of headaches, and statistically speaking, you're probably already using it in your page/app:
// Use jQuery.ajax to get the latest exchange rates, with JSONP:
$.ajax({
url: 'http://openexchangerates.org/api/latest.json?app_id=YOUR_APP_ID',
dataType: 'jsonp',
success: function(json) {
// Rates are in `json.rates`
// Base currency (USD) is `json.base`
// UNIX Timestamp when rates were collected is in `json.timestamp`
// If you're using money.js, do this:
fx.rates = json.rates;
fx.base = json.base;
}
});
Use of JSONP is optional - jQuery will append a callback parameter to the URL, and the response will be wrapped in a function call. This is to prevent access-control (CORS) issues and will save you some headaches in many cases, though for security it's not tip-top (actually, if security is a primary concern in your app, you should proxy the results on your own server to prevent 100% against XSS attacks. Open Exchange Rates will support HTTPS soon, too.
The success callback is asynchronous - meaning that if you have code to be run straight away, which relies on the exchange rates being available, this code should be inside the callback. The rest of your program will keep on executing while that AJAX request is waiting.
强调一下: 如果您有代码可以立即 运行,这取决于可用的汇率,此代码应该是在回调中