2016-06-20 59 views
3

我试图做的终极版认证POST请求,我通过电子邮件&密码的身体,但它在控制台返回此错误:取POST输入意外结束Erorr

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,我看到它在网络选项卡,我看到响应太多,但url和配置后的读取请求在.then(response =>停止。

已经两天了,仍然找不到解决办法。顺便说一下,这在Postman(Chrome扩展)中工作正常,所以没有错误的API。

感谢

答案编辑:这个问题是涉及到CORS,因此对于有同样的问题,像我一样的人。

+1

什么是'response.json()'? –

+0

@RIYAJKHAN这是API的回应,并包含有关请求的大量信息,例如无论成功或不成功,数据如果成功等 – Khpalwalk

+0

但是你对responce.Isnt使用json方法吗? –

回答

0

反正你可以简化你的代码片段到:

fetch('http://localhost:4000/api/authenticate', config) 
    .then(response => response.json()) 
    .then(({ user, response }) => { 

你应该可以添加捕获方法来处理错误的对象。

0

我解决了同样的问题,当我从配置中删除

mode: 'no-cors'