I need to send fetch request(using java script), and when I sending it from localhost:8080 it's fine, but when I'm changing localhost to other - there is 303 response or no response at all.
Also it's including cookies when I sending it from 8080, and NOT sending from other host.
Where could be problem? I'm totally lost.
I need to send fetch request(using java script), and when I sending it from localhost:8080 it's fine, but when I'm changing localhost to other - there is 303 response or no response at all.
Also it's including cookies when I sending it from 8080, and NOT sending from other host.
Where could be problem? I'm totally lost.
Share Improve this question asked Jul 21, 2017 at 15:17 EvgeniiEvgenii 4471 gold badge13 silver badges26 bronze badges 1- 3 How would we know what the problem is without seeing your code? – Daniel Commented Jul 21, 2017 at 15:18
1 Answer
Reset to default 4MDN has some quite good documentation on CORS and fetch
.
When you call fetch on a different origin with a CORS flag:
fetch(url, {method: 'GET', mode: 'cors'})
if the url is from a different origin, fetch
will first issue an OPTION
request with headers:
Origin: http://foo.example
Access-Control-Request-Method: GET
The server must confirm that origin is allowed for such requests, sending response:
Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: GET
Most likely your server does not have CORS enabled. This can be easily checked via browser console network requests. Check that OPTIONS
is sent and replied to by the server, also that your site is present in the Access-Control-Allow-Origin:
header municated back.
Please note, if your server requires authorization, you should call fetch
with {credentials: 'include'}
:
fetch(url, {method: 'GET', mode: 'cors', credentials: 'include'})