Use of the fetch function-FAQ and faq of the fetch Function
Use of fetch:
Overall format:
Fetch ('url ',
{
Mode: 'cors ',
Method: 'post ',
Headers :{
'Content-type': 'application/x-www-form-urlencoded; charset = UTF-8 ', [need to specify]
},
Body: JSON. stringify ({a: '000000 '}),
},). Then ();
Verify that the front and back ends are strings in JSON format.
Note: 'content-type': there are only three types of form encoding formats.
① Text/plain (default): form data in plain text format
② Application/x-www-form-urlencoded and multipart/form-data (form data is encoded as name/value pairs. You do not need to select them in ajax, will help me select the encoding method intelligently. here we need to develop the encoding method)
③ The multipart/form-data Browser splits the entire form into controls.
Among them, the main problems encountered by the frontend and backend communication using fetch are their cross-domain problems, and the pitfalls that I will not learn for a long time in c.
Here I will share my problems and solutions:
(1) fetch request 415 UnsupportedMedia Type problem [see Figure]: Here we only need to modify the encoding Type.
'Content-type': 'application/x-www-form-urlencoded; charset = UTF-8 ',
(2) 401 Unauthorized: no access permission. Deploy IIS and enable Anonymous Access.
Note network (you must re-enable Anonymous Access for each redeployment)
It will still be output in the console
However, the core issue is not cross-origin. Let's take a look at the cross-origin issue.
(3) 405 Method Not Allowed cross-origin access :( cause: access other services by localhost: 3000 under react Service)
Console or output
It is found that there is no Code State and sometimes the problem is more accurate.
Here we provide a solution to the Cross-origin problem of webapi: Recommended Link
Http://www.cnblogs.com/landeanfen/p/5177176.html