Hi All,
I’m trying to use a couple of public API’s to get currency rate. One is from OANDA and the other is from the Central Bank of Argentina.
Neither one fetch ok, giving the message TypeError: Failed to fetch when in debug mode.
The page for testing purpose is https://www.propietariovende.online/estamos-trabajando
The error arises when you enter a number value in #imput1 field.
Both codes are as follow:
OANDA:
export function input1_changeOANDA(event) {
//OANDA API:
let the_appi = “https://www1.oanda.com/rates/api/v2/rates/spot.json”
let api_key = “kH7ccQB4L5rStAJu0mIGEpQh”;
let myInit = {
“meta”: {
“effective_params”: {
“data_set”: “OANDA”,
“base_currencies”: [“USD”],
“quote_currencies”: [“ARS”],
“interbank”: “5.25%”
},
“endpoint”: “spot”,
“request_time”: “2019-12-26T09:33:04+00:00”,
“skipped_currency_pairs”:
}
};
fetch(the_appi, myInit)
.then((httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
} else {
return Promise.reject(“Promesa rechazada”);
}
})
.then(json => $w(‘#text39’).text = json.v)
. catch (err => console.log("Ocurrió un error con la petición: " + err));
}
BCRA:
export function input1_change(event) {
//basado en https://github.com/fedemoglia/variables-economicas-API-datos/blob/master/DolarApiBCRA.gs
let the_appi = “https://api.estadisticasbcra.com/usd_of_minorista”
let accessToken = “eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MDg3NTk3MjgsInR5cGUiOiJleHRlcm5hbCIsInVzZXIiOiJhbGVqYW5kcm8uc2l0b0BnbWFpbC5jb20ifQ.uJb6gPlOxv8gEgHmJJFfsBgaxsxHkyReZeLX3NkNoMWovvO5Oo_M8rA2k2-nvCaXaM3nqYv-sQU3Ahk2QvRQYQ”;
console.log(httpGet(the_appi, accessToken));
// return httpGet(the_appi, accessToken);
function httpGet(url, key) {
const headers = { ‘Authorization’: ‘BEARER ’ + key };
var options = {
‘method’: “GET”,
‘headers’: headers
}
fetch(url, options)
.then((httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json()
} else {
return Promise.reject(“Promesa rechazada”)
}
})
.then(json => { $w(’#text39’).text = $w(‘#text39’).text = json.v;
console.log(json.v) })
. catch (err => console.log("Ocurrió un error con la petición: " + err));
return “Resultado de la función httpGet”
}
}
No problems with other functions that use fetch in that page.
Could someone help me with this issue?
Thanks