2017-08-07 39 views
0

当我在控制台中检查它时,我从提取中获得了这种响应,如图所示。但是,当我从devtool Network检查时,它显示正确的错误响应。有关如何处理这个问题的任何想法?为什么取得这样的回应?

export function loginRequest(data){ 
    return (dispatch, getState) => { 
    let tmp = Object.assign({},data)  
    var request = new Request('https://aaa.com/json', { 
     method: 'POST', 
     mode: 'cors', 
     headers: new Headers({ 
     'Content-Type': 'text/plain' 
     }) 
    }); 
    fetch(request).then((res)=>{   
     alert(JSON.stringify(res)) 
     dispatch({ 
     type: types.LOGIN, 
     data: res 
     }) 
    }).catch(err =>{ 

      alert(JSON.stringify(err)) 
      alert(err.errMsg) 
      dispatch({ 
      type: types.LOGIN, 
      data: data 
     }) 
     console.log(JSON.stringify(err)) 
    }) 

    } 
} 

enter image description here

+0

那么问题是什么? – choasia

+0

尝试提供[mcve]。 – Quentin

回答

0

您需要解析到JSON的反应,看看符合你在网络选项卡中查看响应。

fetch('//offline-news-api.herokuapp.com/stories') 
// First you can deal with the response, checking status code, headers, etc. 
.then(function(response) { 
    if (response.status >= 400) { 
     throw new Error("Bad response from server"); 
    } 

    // This is the line you are missing 
    return response.json(); 
}) 
// Now you will see the proper JSON response that should match 
// what you see in the network tab. 
.then(function(stories) { 
    console.log(stories); 
}); 

使用fetch,您可以看到原始的反应,这是你的截图看来什么是,然后你可以将其解析到JSON查看响应的身体,你期待。这允许更多的微调的错误处理,响应控制等

0

您还可以使用axios,你不必解析您的response,不具备手动reject如果有400错误,它会去改为catch