React is used in the

project, which needs to use fetch instead of Ajax.

, because react's create_react_app tool is very convenient, basically opens the box to use. After creating the project and entering the NPM start command, it automatically listens to a port of 3000, and the front part is ready.

specific reference:

backend part I use Phalcon. The

before and after the end of separation, for convenience, I try to make the same domain in nginx (front and back of the top API, but the same domain name) Phalcon framework is a single entrance, react monitor 3000 times by nginx reverse proxy, JS can not find the problem, then give up to the same domain.

so I configured two domain name:


first name for react, 3000 is the port number (for debugging, officially launched 80)
second domain name for API 80

, the port number is so cross domain problem.

cors details please see:

is allowed to cross domain domain name access

 set $origin = isset part PHP ($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN']: '; $allowOrigin = ['','']; if (in_array ($origin, $allowOrigin) {header ('Access-Control-Allow-Origin:'). $origin header ('Access-Control-Allow-Methods:);} PUT, POST, GET, DELETE, OPTIONS'); header ('Access-Control-Allow-Credentials: true'); header ('Access-Control-Allow-Headers: Content-Type Accept'); 

is part of fetch Ajax

 let postData request (' = {a:'b'}; fetch N/login','POST' mode:'cors'{method:, credentials:,'include', headers: {'Content-Type'}:'application/x-www-form-urlencoded', body: JSON.stringify (postData).Then (function)} (response) {console.log (response);}); all the content above is the pre> < / 

, want to help everyone to learn, I hope you will support the script of the house.

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:Implementation of CORS cross domain request for fetch in react | Script Home

You may also be interested in these articles!