I'm using axios to make GET request to an API, whenever I'm running under the react-create-app development build, the following request works:
axios.get(window.location.href +'/schools/countries/' + country + extra)
But as soon as I run it in the development build I get the following response:
{data: "<!DOCTYPE html><html lang=\"en\"><head><meta charset鈥tic/js/main.08c4cd0c.js\"></script></body></html>", status: 200, statusText: "OK", headers: Object, config: Object, 鈥
The correct response should look like this:
{data: Object, status: 200, statusText: "OK", headers: Object, config: Object, 鈥
I'm using axios to make the GET request. The issue seems to be that its not reaching my back end server ( express server). This is accessed using the proxy parameter in the package.json file:
"proxy": "http://localhost:3001",
How can I fix this error?
Howdy,
As said in the docs, the proxy option in your package.json only has effect in development (when doing yarn start).
So i suggest you have a look at the Deployment section of the docs.
By the way I guess that you want window.location.origin instead of window.location.href, because the latter will include the complete href (basically what you see in your browser's address bar) which might make it so that the URL you end up fetching is not the one you actually want.
PS: Remember to enable CORS on your server otherwise your React app won't be allowed to query it. This link from the docs walks you through it among other topics.
Closing as answered.
Most helpful comment
Howdy,
As said in the docs, the
proxyoption in your package.json only has effect in development (when doingyarn start).So i suggest you have a look at the Deployment section of the docs.
By the way I guess that you want
window.location.origininstead ofwindow.location.href, because the latter will include the complete href (basically what you see in your browser's address bar) which might make it so that the URL you end up fetching is not the one you actually want.PS: Remember to enable CORS on your server otherwise your React app won't be allowed to query it. This link from the docs walks you through it among other topics.