3

enter image description hereMy application having multiple request to different API URL's request, when a particular component renders. I am facing the CORS issue since I am using multiple URLs' , so in order to avoid i haved installed http-proxy-middleware package. In setupProxy.js, I used below code. which is not working Note: I having same endpoints with different api urls's in my project. How deal with this cases

app.use(proxy("/rest/V1/orders",{target:"https://example.com/",changeOrigin:true})); app.use(proxy("/rest/V1/orders",{target:"https://example.net/",changeOrigin:true})); app.use(proxy("/rest/V1/orders",{target:"https://example.org/",changeOrigin:true}));

  • It's tricky to rewrite the URL properly when proxying - are you able to see details of the request that arrives at example.com etc.? What is the exact error? Debugging with an http client like postman always helps btw (to simulate the browser's outgoing request). – timotgl May 11 '20 at 11:11
  • I am getting error intermittently, some times loading without any issue, PFB error Access to XMLHttpRequest at 'https://cors-anywhere.herokuapp.com/xxxxx' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. cors-anywhere.herokuapp.com/https:/xxx Failed to load resource: net::ERR_FAILED createError.js:16 Uncaught (in promise) Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:83) Attached screenshot – T Bharathiraj May 11 '20 at 14:46
  • On the screenshot it looks like you're not sending the request to your own proxy (on localhost), but directly to the target instead? – timotgl May 12 '20 at 14:18
  • Hi i am running app from localhost it is fetching different server, so I used http-proxy.. – T Bharathiraj May 15 '20 at 12:32
  • Right, but the request seems to be going from localhost *to* `cors-anywhere.herokuapp.com` in the screenshot. – timotgl May 16 '20 at 16:02
  • Exactly facing the same issue , i am sending request from localhost to dev or qa spring boot services . As @timotgl you said i am appending the URI `cors-anywhere.herokuapp.com` + environment name + spring boot service name . eg:```https://cors-anywhere.herokuapp.com/https://a1.sky.ie/sky-auth/portalloginuser``` . But my setupProxy app.use is not modifying to the target one. It got failed with my request url http://localhost:9009/sky-auth/portalloginuser 404 not found . can you please help me how to solve or any suggestion – SakthiSureshAnand Nov 01 '20 at 17:01
  • @TBharathiraj how did you solve it. Can you make some sample post – SakthiSureshAnand Nov 01 '20 at 17:02

0 Answers0