I am working on a project where this project is transition from client -> server -> API to client -> API. That is, the project is calling the API from client side completely.
What it looks like before:
Client:
sendCode(){
axios.post("/test",{
})
.then(res => {this.setState({
userID: res.data.userId,
})})
.catch(function (error) {
console.log(error);
});
}
Server(Running on node.js):
app.post('/test',(req, res) => {
//Calling APi from there
axios.post(url).then(response => {
//Getting the data and send to client
}).catch(error => {
//Throwing error
})
})
What it looks like now:
sendCode(){
axios.post(url,{
})
.then(res => {this.setState({
userID: res.data.userId,
})})
.catch(function (error) {
console.log(error);
});
}
Currently, it is getting a error with Acess-Control-Allow-Origin
. However, strangely, i am still getting the data back from the API despite having the error showing up every time.
This error did not appear when I call the API from Node.js. However, it appears when I directly call it from client,
the URL is the internal ESB API that I am calling in my company and I have no control of the ESB configuration
What I have tried so far: I have read the Community Wiki and none of them are suitable in my situation. I have also read another stack post Here . It suggest server configuration which is impossible to do from my end like i mentioned above. i tried to use jsonp like it mentioned, but it did not work at all.
This is what I have tried following the second post:
var postConfig ={
method: "post",
url: url,
dataType: "jsonp",
}
axios(postConfig)
.then(res => {this.setState({
userID: res.data.userId,
})})
.catch(function (error) {
console.log(error);
});
}
Any help is appreciated
I disagree with the flag down. The post suggested following:
- set up a server-side proxy, which is exactly how it was done before and I don't want to do it this way.
- Browser Extension: which is not something I wanted
- Server configuration which is not something that's possible as I explained it.
- Jsonp, which does not work for me.