我想使用JavaScript将POST请求发送到某些服务器。使用here中的代码(即即时创建表单并使用Submit()函数),一切顺利,我将数据取回。
问题:
我需要安静地访问此数据并进行处理,正如您所知,无法从表单的提交中获取响应数据。所以我尝试通过此代码使用访存
const res = await fetch(
url,
{
method: "POST",
body: data,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
mode: "cors"
}
);
但这给了我经典
`Failed to load https://...: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.`
还有这个
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://... with MIME type text/plain. See https://www.chromestatus.com/feature/5629709824032768 for more details.
我该如何克服?提交表单时为什么这不是问题?
已编辑:
使用CORS代理,我可以获得所需的结果。