2016-06-20 101 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'