我正在使用cors -任何地方作为代理服务器的类型,以克服一些cors的问题。我的项目只需要GET和PUT请求。cors -任何地方都解决了我的cors和问题,但是现在使用PUT,我得到了500个错误。下面是我如何设置服务器并发出PUT请求的方式:
//setting up server
var host = process.env.HOST || '0.0.0.0';
var cors_proxy = require('cors-anywhere');
cors_proxy.createServer({
originWhitelist: [], // Allow all origins
removeHeaders: ['cookie', 'cookie2']
}).listen(8000, host, function() {
console.log('Running CORS Anywhere on ' + host + ':' + 8000);
});
//making call
let result = await fetch(`http://localhost:8000/${apiUrl}`, {
method: 'PUT",
mode: 'cors',
body: JSON.stringify({"value":"true"})
});
let json = await result.json();
console.log(json);这里的所有内容都适用于GET请求,并在通过我的TypeScript和JavaScript项目执行PUT请求时给出了一个500个错误。如果我使用具有相同URL的邮递员,一切都会很完美。对发生了什么事有什么想法吗?
谢谢你的帮助。
发布于 2021-07-13 20:47:09
刚刚解决了问题。我没有在PUT fetch请求头中声明内容类型。正确的一个看起来是这样的:
let result = await fetch(`http://localhost:8000/${apiUrl}`, {
method: 'PUT",
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({"value":"true"})
});https://stackoverflow.com/questions/68368100
复制相似问题