最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CORS fetch for different ports - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 4

MDN 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'})
发布评论

评论列表(0)

  1. 暂无评论