【问题标题】:Reactjs isomorphic-fetch PATCH how to have the body error?Reactjs isomorphic-fetch PATCH 如何出现正文错误?
【发布时间】:2016-05-26 18:43:59
【问题描述】:

所以基本上我使用 fetch POST 或 PATCH 方法发送数据,当我遇到错误时,我可以在网络中看到 -> 响应此错误:

{
  "Errors": [
   {
     "Code": -201,
     "Message": "Could not get file from link",
     "AssociatedError": "404 Not Found"
   }
 ],
 "Result": null
}

这是我的实际代码:

function checkStatus(response) {
  if (response.status >= 200 && response.status < 300) {
    return response;
  } else {
    var error = new Error(response.statusText);
    error.response = response;
    throw error;
  }
}

export function sendImageUploaded(data, valueMethod, endpoint) {
    return dispatch => {
        dispatch(requestPosts(data));
        return fetch(endpoint, {
            method: valueMethod,
            headers: new Headers({
                            Authorization: Isengard.config.token
                        }),
            body: data
        })
        .then(checkStatus)
        .then(reponse => {
            dispatch(successSent("The output list has been successfully sent!"));
        }).catch(err => {
            console.log('request failed', err);
            dispatch(failSent("Error on sending request: " + err));
        });
    };
};

我正在为收到此错误消息而苦苦挣扎。

【问题讨论】:

    标签: javascript reactjs xmlhttprequest


    【解决方案1】:

    您已经在“error.response”中收到响应错误。你只需要兑现那个承诺。

    而不是

    .catch(err => {
                    console.log('request failed', err);
                    dispatch(failSent("Error on sending request: " + err));
    });
    

    使用

    .catch(err => {
            err.response.json().then((json) =>{
               let {Errors,Result} = json;
               dispatch(failSent(Errors)); // You are sending your array of errors here
            });
    });
    

    【讨论】:

      猜你喜欢
      • 2019-05-25
      • 2016-10-22
      • 2020-08-12
      • 2020-12-04
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      相关资源
      最近更新 更多