我正在尝试在 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));
}
});
};
}
获取 POST 方法调用 API,我在网络选项卡中看到它,我也看到响应,但获取请求停止在 .then(response =>
url 和配置之后。
已经两天了,还是没找到解决办法。顺便说一句,这在 Postman(chrome 扩展)中工作正常,所以 API 没有问题。
谢谢
答案编辑:这个问题与 CORS 有关,所以对于和我有同样问题的人来说。
最佳答案
我删除时解决了同样的问题
mode: 'no-cors'
来自配置。
关于javascript - 获取 POST 输入错误的意外结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37924490/