Access Control Origin Header error using Axios in React Web throwing error in Chrome
up vote
8
down vote
favorite
Im making an API call using Axios in a React Web app. However Im getting the error in Chrome as,
XMLHttpRequest cannot load https://example.restdb.io/rest/mock-data. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
{
axios.get('https://example.restdb.io/rest/mock-data', {
headers: {
'x-apikey': 'API_KEY',
},
responseType: 'json',
}).then(response => {
this.setState({ tableData: response.data });
});
}
I have also read several answers on StackOverflow about the same issue, titled "Access-Control-Allow-Origin" but still coudnt figure out how to solve this. I dont want to use an extension IN Chrome or use a temporary hack to solve this. Please suggest the standard way of solving the above issue.
After trying out few answers I have tried with this,
headers: {
'x-apikey': '59a7ad19f5a9fa0808f11931',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
},
Now I get the error as,
Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response
javascript rest reactjs google-chrome axios
add a comment |
up vote
8
down vote
favorite
Im making an API call using Axios in a React Web app. However Im getting the error in Chrome as,
XMLHttpRequest cannot load https://example.restdb.io/rest/mock-data. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
{
axios.get('https://example.restdb.io/rest/mock-data', {
headers: {
'x-apikey': 'API_KEY',
},
responseType: 'json',
}).then(response => {
this.setState({ tableData: response.data });
});
}
I have also read several answers on StackOverflow about the same issue, titled "Access-Control-Allow-Origin" but still coudnt figure out how to solve this. I dont want to use an extension IN Chrome or use a temporary hack to solve this. Please suggest the standard way of solving the above issue.
After trying out few answers I have tried with this,
headers: {
'x-apikey': '59a7ad19f5a9fa0808f11931',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
},
Now I get the error as,
Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response
javascript rest reactjs google-chrome axios
Check the HTTP response code on the response you’re getting. Are you getting a 200 OK from it? Because when I look at it I see a 503 “Service Unavailable”. Try browsing directly to example.restdb.io/rest/mock-data and I think at least there you’ll see the same thing. So there’s no way your request is going to work if the server responds with a 503. I think the only reason you’re getting the CORS error message is just because many servers typically don’t send the Access-Control-Allow-Origin in 5xx responses or other responses. They only send it with success responses (e.g., 200 OK).
– sideshowbarker
Aug 31 '17 at 9:12
2
Don’t add the Access-Control-Allow-Origin to your request. That header is strictly just a response header for servers to send back to you in responses. The only effect adding it to a request will have is to break things. Same for the Access-Control-Allow-Methods header. Adding those to your request isn’t ever going to prevent the browser for running into the first error cited in the question.
– sideshowbarker
Aug 31 '17 at 9:13
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
Im making an API call using Axios in a React Web app. However Im getting the error in Chrome as,
XMLHttpRequest cannot load https://example.restdb.io/rest/mock-data. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
{
axios.get('https://example.restdb.io/rest/mock-data', {
headers: {
'x-apikey': 'API_KEY',
},
responseType: 'json',
}).then(response => {
this.setState({ tableData: response.data });
});
}
I have also read several answers on StackOverflow about the same issue, titled "Access-Control-Allow-Origin" but still coudnt figure out how to solve this. I dont want to use an extension IN Chrome or use a temporary hack to solve this. Please suggest the standard way of solving the above issue.
After trying out few answers I have tried with this,
headers: {
'x-apikey': '59a7ad19f5a9fa0808f11931',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
},
Now I get the error as,
Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response
javascript rest reactjs google-chrome axios
Im making an API call using Axios in a React Web app. However Im getting the error in Chrome as,
XMLHttpRequest cannot load https://example.restdb.io/rest/mock-data. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
{
axios.get('https://example.restdb.io/rest/mock-data', {
headers: {
'x-apikey': 'API_KEY',
},
responseType: 'json',
}).then(response => {
this.setState({ tableData: response.data });
});
}
I have also read several answers on StackOverflow about the same issue, titled "Access-Control-Allow-Origin" but still coudnt figure out how to solve this. I dont want to use an extension IN Chrome or use a temporary hack to solve this. Please suggest the standard way of solving the above issue.
After trying out few answers I have tried with this,
headers: {
'x-apikey': '59a7ad19f5a9fa0808f11931',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
},
Now I get the error as,
Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response
javascript rest reactjs google-chrome axios
javascript rest reactjs google-chrome axios
edited Aug 31 '17 at 7:20
asked Aug 31 '17 at 6:58
SeaWarrior404
61111430
61111430
Check the HTTP response code on the response you’re getting. Are you getting a 200 OK from it? Because when I look at it I see a 503 “Service Unavailable”. Try browsing directly to example.restdb.io/rest/mock-data and I think at least there you’ll see the same thing. So there’s no way your request is going to work if the server responds with a 503. I think the only reason you’re getting the CORS error message is just because many servers typically don’t send the Access-Control-Allow-Origin in 5xx responses or other responses. They only send it with success responses (e.g., 200 OK).
– sideshowbarker
Aug 31 '17 at 9:12
2
Don’t add the Access-Control-Allow-Origin to your request. That header is strictly just a response header for servers to send back to you in responses. The only effect adding it to a request will have is to break things. Same for the Access-Control-Allow-Methods header. Adding those to your request isn’t ever going to prevent the browser for running into the first error cited in the question.
– sideshowbarker
Aug 31 '17 at 9:13
add a comment |
Check the HTTP response code on the response you’re getting. Are you getting a 200 OK from it? Because when I look at it I see a 503 “Service Unavailable”. Try browsing directly to example.restdb.io/rest/mock-data and I think at least there you’ll see the same thing. So there’s no way your request is going to work if the server responds with a 503. I think the only reason you’re getting the CORS error message is just because many servers typically don’t send the Access-Control-Allow-Origin in 5xx responses or other responses. They only send it with success responses (e.g., 200 OK).
– sideshowbarker
Aug 31 '17 at 9:12
2
Don’t add the Access-Control-Allow-Origin to your request. That header is strictly just a response header for servers to send back to you in responses. The only effect adding it to a request will have is to break things. Same for the Access-Control-Allow-Methods header. Adding those to your request isn’t ever going to prevent the browser for running into the first error cited in the question.
– sideshowbarker
Aug 31 '17 at 9:13
Check the HTTP response code on the response you’re getting. Are you getting a 200 OK from it? Because when I look at it I see a 503 “Service Unavailable”. Try browsing directly to example.restdb.io/rest/mock-data and I think at least there you’ll see the same thing. So there’s no way your request is going to work if the server responds with a 503. I think the only reason you’re getting the CORS error message is just because many servers typically don’t send the Access-Control-Allow-Origin in 5xx responses or other responses. They only send it with success responses (e.g., 200 OK).
– sideshowbarker
Aug 31 '17 at 9:12
Check the HTTP response code on the response you’re getting. Are you getting a 200 OK from it? Because when I look at it I see a 503 “Service Unavailable”. Try browsing directly to example.restdb.io/rest/mock-data and I think at least there you’ll see the same thing. So there’s no way your request is going to work if the server responds with a 503. I think the only reason you’re getting the CORS error message is just because many servers typically don’t send the Access-Control-Allow-Origin in 5xx responses or other responses. They only send it with success responses (e.g., 200 OK).
– sideshowbarker
Aug 31 '17 at 9:12
2
2
Don’t add the Access-Control-Allow-Origin to your request. That header is strictly just a response header for servers to send back to you in responses. The only effect adding it to a request will have is to break things. Same for the Access-Control-Allow-Methods header. Adding those to your request isn’t ever going to prevent the browser for running into the first error cited in the question.
– sideshowbarker
Aug 31 '17 at 9:13
Don’t add the Access-Control-Allow-Origin to your request. That header is strictly just a response header for servers to send back to you in responses. The only effect adding it to a request will have is to break things. Same for the Access-Control-Allow-Methods header. Adding those to your request isn’t ever going to prevent the browser for running into the first error cited in the question.
– sideshowbarker
Aug 31 '17 at 9:13
add a comment |
3 Answers
3
active
oldest
votes
up vote
6
down vote
accepted
If your backend support CORS, you probably need to add to your request this header:
headers: {"Access-Control-Allow-Origin": "*"}
If not - you must ensure the server can manage the CORS-requests.
You can find documentation about this mechanism here:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
1
I have added it , 'Access-Control-Allow-Origin' : '*', 'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS', However I get the error now as "Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response" Im using the restdb.io as my api end point.
– SeaWarrior404
Aug 31 '17 at 7:18
have you done the settings for CORS like in this example? restdb.io/docs/apikeys-and-cors
– Vlad Povalii
Aug 31 '17 at 7:21
add a comment |
up vote
0
down vote
Using the Access-Control-Allow-Origin header to the request won't help you in that case while this header can only be used on the response...
To make it work you should probably add this header to your response.You can also try to add the header crossorigin:true
to your request.
add a comment |
up vote
0
down vote
As I understand the problem is that request is sent from localhost:3000 to localhost:8080 and browser rejects such requests as CORS. So solution was to create proxy
My solution was :
import proxy from 'http-proxy-middleware'
app.use('/api/**', proxy({ target: "http://localhost:8080" }));
New contributor
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
If your backend support CORS, you probably need to add to your request this header:
headers: {"Access-Control-Allow-Origin": "*"}
If not - you must ensure the server can manage the CORS-requests.
You can find documentation about this mechanism here:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
1
I have added it , 'Access-Control-Allow-Origin' : '*', 'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS', However I get the error now as "Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response" Im using the restdb.io as my api end point.
– SeaWarrior404
Aug 31 '17 at 7:18
have you done the settings for CORS like in this example? restdb.io/docs/apikeys-and-cors
– Vlad Povalii
Aug 31 '17 at 7:21
add a comment |
up vote
6
down vote
accepted
If your backend support CORS, you probably need to add to your request this header:
headers: {"Access-Control-Allow-Origin": "*"}
If not - you must ensure the server can manage the CORS-requests.
You can find documentation about this mechanism here:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
1
I have added it , 'Access-Control-Allow-Origin' : '*', 'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS', However I get the error now as "Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response" Im using the restdb.io as my api end point.
– SeaWarrior404
Aug 31 '17 at 7:18
have you done the settings for CORS like in this example? restdb.io/docs/apikeys-and-cors
– Vlad Povalii
Aug 31 '17 at 7:21
add a comment |
up vote
6
down vote
accepted
up vote
6
down vote
accepted
If your backend support CORS, you probably need to add to your request this header:
headers: {"Access-Control-Allow-Origin": "*"}
If not - you must ensure the server can manage the CORS-requests.
You can find documentation about this mechanism here:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
If your backend support CORS, you probably need to add to your request this header:
headers: {"Access-Control-Allow-Origin": "*"}
If not - you must ensure the server can manage the CORS-requests.
You can find documentation about this mechanism here:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
answered Aug 31 '17 at 7:16
Vlad Povalii
461313
461313
1
I have added it , 'Access-Control-Allow-Origin' : '*', 'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS', However I get the error now as "Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response" Im using the restdb.io as my api end point.
– SeaWarrior404
Aug 31 '17 at 7:18
have you done the settings for CORS like in this example? restdb.io/docs/apikeys-and-cors
– Vlad Povalii
Aug 31 '17 at 7:21
add a comment |
1
I have added it , 'Access-Control-Allow-Origin' : '*', 'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS', However I get the error now as "Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response" Im using the restdb.io as my api end point.
– SeaWarrior404
Aug 31 '17 at 7:18
have you done the settings for CORS like in this example? restdb.io/docs/apikeys-and-cors
– Vlad Povalii
Aug 31 '17 at 7:21
1
1
I have added it , 'Access-Control-Allow-Origin' : '*', 'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS', However I get the error now as "Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response" Im using the restdb.io as my api end point.
– SeaWarrior404
Aug 31 '17 at 7:18
I have added it , 'Access-Control-Allow-Origin' : '*', 'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS', However I get the error now as "Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response" Im using the restdb.io as my api end point.
– SeaWarrior404
Aug 31 '17 at 7:18
have you done the settings for CORS like in this example? restdb.io/docs/apikeys-and-cors
– Vlad Povalii
Aug 31 '17 at 7:21
have you done the settings for CORS like in this example? restdb.io/docs/apikeys-and-cors
– Vlad Povalii
Aug 31 '17 at 7:21
add a comment |
up vote
0
down vote
Using the Access-Control-Allow-Origin header to the request won't help you in that case while this header can only be used on the response...
To make it work you should probably add this header to your response.You can also try to add the header crossorigin:true
to your request.
add a comment |
up vote
0
down vote
Using the Access-Control-Allow-Origin header to the request won't help you in that case while this header can only be used on the response...
To make it work you should probably add this header to your response.You can also try to add the header crossorigin:true
to your request.
add a comment |
up vote
0
down vote
up vote
0
down vote
Using the Access-Control-Allow-Origin header to the request won't help you in that case while this header can only be used on the response...
To make it work you should probably add this header to your response.You can also try to add the header crossorigin:true
to your request.
Using the Access-Control-Allow-Origin header to the request won't help you in that case while this header can only be used on the response...
To make it work you should probably add this header to your response.You can also try to add the header crossorigin:true
to your request.
answered Aug 9 at 13:37
Chuck-Durst
11
11
add a comment |
add a comment |
up vote
0
down vote
As I understand the problem is that request is sent from localhost:3000 to localhost:8080 and browser rejects such requests as CORS. So solution was to create proxy
My solution was :
import proxy from 'http-proxy-middleware'
app.use('/api/**', proxy({ target: "http://localhost:8080" }));
New contributor
add a comment |
up vote
0
down vote
As I understand the problem is that request is sent from localhost:3000 to localhost:8080 and browser rejects such requests as CORS. So solution was to create proxy
My solution was :
import proxy from 'http-proxy-middleware'
app.use('/api/**', proxy({ target: "http://localhost:8080" }));
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
As I understand the problem is that request is sent from localhost:3000 to localhost:8080 and browser rejects such requests as CORS. So solution was to create proxy
My solution was :
import proxy from 'http-proxy-middleware'
app.use('/api/**', proxy({ target: "http://localhost:8080" }));
New contributor
As I understand the problem is that request is sent from localhost:3000 to localhost:8080 and browser rejects such requests as CORS. So solution was to create proxy
My solution was :
import proxy from 'http-proxy-middleware'
app.use('/api/**', proxy({ target: "http://localhost:8080" }));
New contributor
edited Nov 21 at 20:21
cfnerd
2,24872330
2,24872330
New contributor
answered Nov 21 at 19:53
LvDevR1
1
1
New contributor
New contributor
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45975135%2faccess-control-origin-header-error-using-axios-in-react-web-throwing-error-in-ch%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Check the HTTP response code on the response you’re getting. Are you getting a 200 OK from it? Because when I look at it I see a 503 “Service Unavailable”. Try browsing directly to example.restdb.io/rest/mock-data and I think at least there you’ll see the same thing. So there’s no way your request is going to work if the server responds with a 503. I think the only reason you’re getting the CORS error message is just because many servers typically don’t send the Access-Control-Allow-Origin in 5xx responses or other responses. They only send it with success responses (e.g., 200 OK).
– sideshowbarker
Aug 31 '17 at 9:12
2
Don’t add the Access-Control-Allow-Origin to your request. That header is strictly just a response header for servers to send back to you in responses. The only effect adding it to a request will have is to break things. Same for the Access-Control-Allow-Methods header. Adding those to your request isn’t ever going to prevent the browser for running into the first error cited in the question.
– sideshowbarker
Aug 31 '17 at 9:13