【问题标题】:Fetch http POST request sending EMPTY object to Node server获取发送 EMPTY 对象到 Node 服务器的 http POST 请求
【发布时间】:2020-03-07 07:23:48
【问题描述】:

我正在尝试从 html 表单向我的 Node 服务器发出 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);
    });

【问题讨论】:

标签: javascript html node.js express fetch


【解决方案1】:

您说的是mode: 'no-cors',因此所有需要CORS 权限的内容都会被忽略(包括将Content-Type 标头设置为application/json)。

删除mode: 'no-cors'(并在服务器上使用cors middleware)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-18
    • 2014-06-29
    • 2014-09-02
    • 1970-01-01
    相关资源
    最近更新 更多