Create-react-app: Making GET request using create-react-app production build

Created on 30 Nov 2017  路  2Comments  路  Source: facebook/create-react-app

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?

question

Most helpful comment

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.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dualcnhq picture dualcnhq  路  3Comments

JimmyLv picture JimmyLv  路  3Comments

xgqfrms-GitHub picture xgqfrms-GitHub  路  3Comments

Evan-GK picture Evan-GK  路  3Comments

adrice727 picture adrice727  路  3Comments