提出这样的要求:
return fetch(
'http://localhost:8000/login',
{ method: 'POST',
headers: new Headers(
{"Content-Type": "application/json",
"Accept":"application/json"}
),
body: JSON.stringify(
{'name': 'Tom', 'password': 'Soyer'}
)
}
).then( response => { console.log(response);})
.catch(err => console.log(err))
Run Code Online (Sandbox Code Playgroud)
请求使用OPTIONS方法运行而不是POST.仅在添加模式时:'no-cors'请求变为POST:
return fetch(
'http://localhost:8000/login',
{ method: 'POST',
mode: 'no-cors',
headers: new Headers(
{"Content-Type": "application/json",
"Accept":"application/json"}
),
body: JSON.stringify(
{'name': 'Tom', 'password': 'Soyer'}
)
}
).then( response => { console.log(response);})
.catch(err => console.log(err))
Run Code Online (Sandbox Code Playgroud)
但响应不好(即使网络响应状态为200):{type:"opaque",url:"",status:0,ok:false,statusText:""...}我想是因为
Content-Type标头唯一允许的值是:application/x-www-form-urlencoded multipart/form-data text/plain
这里描述https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
是否有任何方式可以使用fetch生成POST json数据?