I am trying to show the result from call backend response on the home page of a site. The value fetched from the backend must show automatically as soon as page loads.
backend code:
import { getJSON } from ‘wix-fetch’ ;
export function getUFvalue () {
getJSON ( ‘https://api.santa.cl/uf’ )
. then ( json => {
console . log ( json )
const UFvaluetoday = json . uf ;
console . log ( UFvaluetoday )
return UFvaluetoday ;
})
. catch ( err => console . log ( err ));
}
Frontend code:
import { getUFvalue } from ‘backend/getUFdata’ ;
onReady ( function () {
getUFvalue ()
. then ( function ( result ) {
$w ( '#txtUFtoday' ). text = `UF value today is: ${ result } ` ;
console . log ( result );
});
});
1 Like
it returns undefined on frontend
Try this one…
import { getUFvalue } from 'backend/getUFdata';
$w.onReady(()=>{
getUFvalue()
.then((result)=>{console.log(result);
$w('#txtUFtoday').text = `UF value today is: ${result}`;
});
});
import { getJSON } from 'wix-fetch';
export function getUFvalue(){
return getJSON('https://api.santa.cl/uf')
.then(json => {console.log(json);
const UFvaluetoday = json.uf; console.log(UFvaluetoday);
return UFvaluetoday;
}).catch(err => console.log(err));
}
can you help me with the HTML code to get UF price? please
Seems like this has been answered above. Explain what you are trying to do, what works, and what doesn’t. Also, add any code in a code block as stated in the Forum Guidelines .
@yisrael-wix hi!! i am trying to get UF price (CLF) in my wix site. incrusted HTML. from here mindicador. cl/
@carolasanchez You will need to consult with the service provider to find out how to use their API or embedded HTML solution.
@yisrael-wix $.getJSON(‘https:// mindicador. cl/api’, function(data) {
var dailyIndicators = data;
$(“.valor-uf”).html(‘$’ + dailyIndicators. uf.valor );
}).fail(function() {
console.log(‘Error al consumir la API!’);
});
@yisrael-wix i cant get it how…
@yisrael-wix and javascript code to html?
You might be able to use an HtmlComponent . For more information, see the article Working with the HTML Component in Velo . You will need to find out from the service provider how to get it working.
A more secure way to handle web service requests is by using backend (server-side) code. Using backend code you can secure your passwords, API keys, and other secret information. The article Accessing 3rd Party Services explains how this is done. I recommend checking with the service provider to see if they provide a REST interface.