1

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:

  1. set up a server-side proxy, which is exactly how it was done before and I don't want to do it this way.
  2. Browser Extension: which is not something I wanted
  3. Server configuration which is not something that's possible as I explained it.
  4. Jsonp, which does not work for me.
ZpfSysn
  • 807
  • 2
  • 12
  • 30
  • another good resource for you to read: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS – Andrew Lohr Dec 04 '18 at 18:53
  • Hi, Dropping in from the review queue for re-opening. Unfortunately the options in the duplicate *are* the only options when it comes to CORS errors. This means that you're out of luck and your question is still a duplicate. Yes it's frustrating that you need a proxy server to access an internal API, I've been there too. Programming for the web is Fun! – coagmano Dec 05 '18 at 05:25
  • @FredStark Hey, thank you for the efforts. I appreciate your feedback. – ZpfSysn Dec 05 '18 at 15:37
  • @AndrewLohr thanks for the link. It was the first link I found when I encountered this problem and it didn;t help, but still thank you! – ZpfSysn Dec 05 '18 at 15:38

0 Answers0