nodejs 400错误请求错误。通过XMLHttpRequest()发送表单数据时发送

时间:2018-06-05 15:36:02

标签: javascript node.js reactjs express

我无法将表单数据发送到本地NodeJS Express服务器。我认为这与我如何格式化我的请求有关。我试图将一个新的用户帐户注册到Postgresql数据库,使用Passport作为中间件,虽然我不相信代码使它成为那么远。

Chrome DevTools网络标签为我提供了有关错误请求的其他信息     {" status":"无法读取属性'长度'未定义"}

当用户点击“创建帐户”时,会触发此代码:

processForm(event) {
// prevent default action. in this case, action is the form submission event
event.preventDefault();

// create a string for an HTTP body message
const name = encodeURIComponent(this.state.user.name);
const email = encodeURIComponent(this.state.user.email);
const password = encodeURIComponent(this.state.user.password);
const formData = `name=${name}&email=${email}&password=${password}`;

// create an AJAX request
const xhr = new XMLHttpRequest();
xhr.open('post', '/auth/register');
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.responseType = 'json';
xhr.addEventListener('load', () => {
  if (xhr.status === 200) {
    // success

    // change the component-container state
    this.setState({
      errors: {}
    });

    // set a message
    localStorage.setItem('successMessage', xhr.response.message);
    this.setState({redirect: true});

  } else {
    // failure

    const errors = xhr.response.errors ? xhr.response.errors : {};
    errors.summary = xhr.response.message;

    this.setState({
      errors
    });
  }
});
xhr.send(formData);
}

Chrome DevTools会闪现&x; xhr.send(formData)'作为堆栈中的第一个错误。

这是快递处理程序代码,但我认为它无法实现这一目标:

router.post('/register', authHelpers.loginRedirect, (req, res, next)  
=> {
return authHelpers.createUser(req, res)
.then((response) => {
    passport.authenticate('local', (err, user, info) => {
      if (user) { handleResponse(res, 200, 'success'); }
    })(req, res, next);
})
.catch((err) => { handleResponse(res, 500, 'error'); });
});

非常感谢任何帮助。我花了好几个小时试图解决它。这就像每次stackoverflow发布一样。

0 个答案:

没有答案