0

我正在进行 fetch api 调用。当我直接使用 json 数据进行调用时,我得到了响应,但是当我使用相关的所需标头进行 api 调用时,我收到了诸如意外的 toke o或 SyntaxError: Unexpected token < in JSON at position 0 之类的错误。

我尝试使用JSON.parse(response)然后尝试json.response()但似乎没有任何效果。

export const trackingInfoFetchData= url => {
   return (dispatch) => {
       dispatch(trackingInfoIsLoading(true));
        fetch(url, headers:{headers})
       .then((response) => {
           if (!response.ok) {
               throw Error(response.statusText);
           }
           dispatch(trackingInfoIsLoading(false));
           return response;
       })
       .then((response) => response.json())
       .then((items) =>{
            dispatch(trackingInfoFetchDataSuccess(items))
          })
      .catch(error => {
        dispatch(trackingInfoHasErrored(true));
        const errMsg = 'Unknown Error Occurred: '.concat(error.message);
        dispatch(displayError(errMsg));
    })
  };
4

0 回答 0