当我在控制台中运行post请求时,它是这样显示的。
Access to fetch at 'http://dummy.restapiexample.com/api/v1/create' from origin 'http://127.0.0.1:5501' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
tut42.js:57 POST http://dummy.restapiexample.com/api/v1/create net::ERR_FAILED
postData @ tut42.js:57
(anonymous) @ tut42.js:61
tut42.js:57 Uncaught (in promise) TypeError: Failed to fetch
at postData (tut42.js:57)
at tut42.js:61
postData @ tut42.js:57
(anonymous) @ tut42.js:61
Promise.then (async)
postData @ tut42.js:58
(anonymous) @ tut42.js:61发布于 2022-01-09 11:16:37
作为跨域请求,API调用本身似乎被阻塞,您可以尝试单独添加访问控制允许标头,也可以切换到无cors模式。
fetch(request, {mode: 'cors'});为了更好地理解CORS,请查看https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS。
对于可以与访问控制允许标头对应的不同标题,请在下面的链接https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers中显示
Access-Control-Allow-Headers: *https://stackoverflow.com/questions/70640677
复制相似问题