javascript - 获取发送 EMPTY 对象到 Node 服务器的 http POST 请求
问题描述
我正在尝试从 html 表单向我的节点服务器发出 HTTP Post 请求。我使用 Body Parser 和所有东西,但是当我尝试获取请求并将 req.body 记录在我的服务器中时,它是一个 EMPTY 对象。你知道我该如何解决这个问题吗?
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const form = document.querySelector('form');
form.addEventListener('submit', e => {
e.preventDefault();
const email = document.querySelector('#email').value;
const password = document.querySelector('#password').value;
const formData = { email, password };
const options = {
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify(formData),
mode: 'no-cors'
};
fetch('http://localhost:3000/users/login', options)
.then(res => {
console.log(res);
res.json();
})
.then(data => {
console.log(data);
});
解决方案
您说过mode: 'no-cors'
,所有需要 CORS 权限的内容都将被忽略(包括将Content-Type
标头设置为application/json
)。
删除mode: 'no-cors'
(并使用服务器上的cors 中间件)。
推荐阅读
- reactjs - 如何使用 git(使用 http)在 heroku 上推送更改
- r - 将宽数据帧中的值合并到长数据帧中
- oracle - 我可以在不修改 tnsnames.ora 的情况下创建连接吗?
- amazon-web-services - 在 AWS ECS 中设置 ulimit:HardLimit 不能为空
- python - 如何为多列定义变量
- sql - SQL Server 中列值的每行总计
- python - 我收到此代码错误,请任何人纠正并解释我的错误
- vue.js - 无法通过 npm 链接使用 Vue 组件库
- python - 骰子游戏得分和开始不起作用
- reactjs - 通过不更新状态的道具将功能从父组件传递到子组件来关闭 react-bootstrap 弹出模式