before i begin i need to make the statement that I am, self-learning noob ('Hello').
I'm trying to get data from a Croatian API banking site http://api.hnb.hr/tecajn. The call looks like this:
[{
"broj_tecajnice": "162",
"datum": "2017-08-24",
"drzava": "Australija",
"sifra_valute": "036",
"valuta": "AUD",
"jedinica": 1,
"kupovni_tecaj": "4,942956",
"srednji_tecaj": "4,957829",
"prodajni_tecaj": "4,972702"
},{
"broj_tecajnice": "162",
"datum": "2017-08-24",
"drzava": "Kanada",
"sifra_valute": "124",
"valuta": "CAD",
"jedinica": 1,
"kupovni_tecaj": "4,979337",
"srednji_tecaj": "4,994320",
"prodajni_tecaj": "5,009303"
}, ]
The problem I'm facing is No 'Access-Control-Allow-Origin' header
XMLHttpRequest cannot load http://api.hnb.hr/tecajn. Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3000' is therefore not allowed access.
I'm learning and using React
for the UI and axios
for the get request.
import React, { Component } from 'react';
import axios from 'axios';
class App extends Component {
constructor(props) {
super(props);
this.state = {
test: null
}
}
componentDidMount(){
const getData = () => {
const url = ' http://api.hnb.hr/tecajn';
axios.get(url, {
headers: { 'Access-Control-Allow-Origin': '*'}
}).then( (response) => {
console.log("response", response);
this.setState({
test: response.data
});
})
.catch( (error) => {
if(error instanceof Error) {
console.log(error.message);
} else {
console.log(error.data);
}
});
}
getData();
}
render() {
return (
<div>
<div className='container'>
<p>test</p>
</div>
</div>
);
}
}
export default App;
I'we looked at almost all the documentation and i still don't get it.
https://www.html5rocks.com/en/tutorials/cors
https://stackoverflow.com/questions/35588699/response-to-preflight-request-doesnt-pass-access-control-check
I'we tried a lot of combination and i need help. Do i need to use Express.js do i need to do something els. I don't know.
If u good ppl have time to help me i would be grateful.
TY!