Accesso al tasso di cambio tramite JSON API per ciottoli
-
20-12-2019 - |
Domanda
Sto cercando di scrivere una semplice copertura per il ciottolo e sto arrivando contro questo errore JavaScript.
Sto tirando le informazioni da http://rate-exchange.appspot.com / valuta? Da= USD & AMP; a= JPY
Il codice sembra:
function HTTPGET(url) {
var req = new XMLHttpRequest();
req.open("GET", url, false);
req.send(null);
return req.responseText;
}
var getWeather = function() {
var lhs1 = "usd";
var rhs1 = "jpy";
var url1 = "rate-exchange.appspot.com/currency?from=" + lhs1 + "&to=" + rhs1
console.log(url1);
var response1 = HTTPGET(url1);
var json1 = JSON.parse(response1);
.
ecc e va avanti ma hai l'idea.
ottengo questo
[PHONE] pebble-app.js:?: JS: where.is.spot: rate-exchange.appspot.com/currency?from=usd&to=jpy
[PHONE] pebble-app.js:?: Error: where.is.spot: Invalid URL at line 4 in pebble-js-app.js
.
che sta fallendo alla linea 4 qui: req.Send (NULL);
Qualche idea su cosa sta causando questo errore?Non sembra essere qualcosa di sbagliato con il feed.
Soluzione
L'URL dovrebbe iniziare con http://
:
Modifica:
var url1 = "rate-exchange.appspot.com/currency?from=" + lhs1 + "&to=" + rhs1
.
IN:
var url1 = "http://rate-exchange.appspot.com/currency?from=" + lhs1 + "&to=" + rhs1
. Altri suggerimenti
invece di
req.send (null);
Usa
req.send ();
Se vuoi provare con jQuery:
var getCurrencyPair = function() {
var url = 'http://rate-exchange.appspot.com/currency?from=usd&to=jpy'
$.getJSON( url, function( data ) {
console.log(data);
});
};
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow