我正在尝试在Redux中进行POST身份验证请求,并且将电子邮件和密码作为正文传递,但它在控制台中返回此错误:

Uncaught (in promise) SyntaxError: Unexpected end of input

我四处寻找答案,很多人建议这可能是缺少的}大括号,但我一直在寻找它,但我认为不是那样。

这是我的提取功能。
export function loginUser(creds) {

  let config = {
    mode: 'no-cors',
    method: 'POST',
    headers: { 'Content-Type':'application/x-www-form-urlencoded' },
    body: `email=${creds.email}&password=${creds.password}`
  };


  return dispatch => {
    dispatch(requestLogin(creds));
    return fetch('http://localhost:4000/api/authenticate', config)
    .then(response =>
      response.json()
      .then(user => ({ user, response }))
    ).then(({ user, response }) =>  {
      if (!response.ok) {
        dispatch(loginError(user.message));
        return Promise.reject(user);
      } else {
        localStorage.setItem('id_token', user.token);
        dispatch(receiveLogin(user));
      }
    });
  };
}

fetch POST方法调用该API,我在“网络”选项卡中也看到了它,我也看到了响应,但是fetch请求在url和config之后停止在.then(response =>处。

已经两天了,仍然找不到解决方案。顺便说一句,这在Postman(chrome扩展程序)中可以正常工作,因此API没错。

谢谢

答案编辑:该问题与CORS有关,因此对于与我一样具有相同问题的任何人。

最佳答案

删除时我会解决相同的问题

mode: 'no-cors'

从配置。

关于javascript - 提取POST输入错误意外结束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37924490/

10-12 00:26
查看更多