Vue.js 是否有任何方法可以捕获导致Vue js fetch api出现POST 400(错误请求)的响应数据?

Vue.js 是否有任何方法可以捕获导致Vue js fetch api出现POST 400(错误请求)的响应数据?,vue.js,fetch-api,Vue.js,Fetch Api,我通过逐个遍历所有索引从数组发送post请求 function apiService(endpoint, method, data) { // D.R.Y. code to make HTTP requests to the REST API backend using fetch const config = { method: method || "GET", body: data !== undefined ? JSON.stringify(da

我通过逐个遍历所有索引从数组发送post请求

function apiService(endpoint, method, data) {
  // D.R.Y. code to make HTTP requests to the REST API backend using fetch
  const config = {
    method: method || "GET",
    body: data !== undefined ? JSON.stringify(data) : null,
    headers: {
      'content-type': 'application/json',
      'X-CSRFTOKEN': CSRF_TOKEN
    }
  };
  return fetch(endpoint, config)
           .then(handleResponse)
           .catch(error => console.log(error))
}
让len=this.rowObject.length;

对于(var i=0;i,您可以插入一个错误处理程序来捕获所需的信息,如下面的
onError

function apiService(endpoint, method, data, onError) {
  // D.R.Y. code to make HTTP requests to the REST API backend using fetch
  const config = {
    method: method || "GET",
    body: data !== undefined ? JSON.stringify(data) : null,
    headers: {
      'content-type': 'application/json',
      'X-CSRFTOKEN': CSRF_TOKEN
    }
  };
  return fetch(endpoint, config)
           .then(handleResponse)
           .catch(onError)
}

let len = this.rowObject.length;
for (var i = 0; i <len; i++) {
  let onError = error => console.log("Error on rowObject " + i + ": " + error);
  apiService(endpoint, method, this.rowObject[i], onError);                   
}
函数apiService(端点、方法、数据、OneError){
//使用fetch向REST API后端发出HTTP请求的D.R.Y.代码
常量配置={
方法:方法| |“GET”,
body:data!==未定义?JSON.stringify(数据):null,
标题:{
“内容类型”:“应用程序/json”,
“X-CSRFTOKEN”:CSRF\U令牌
}
};
返回获取(端点,配置)
.然后(HandlerResponse)
.catch(onError)
}
设len=this.rowObject.length;
对于(var i=0;i console.log(“rowObject上的错误”+i+:“+Error”);
apiService(端点,方法,this.rowObject[i],onError);
}
function apiService(endpoint, method, data, onError) {
  // D.R.Y. code to make HTTP requests to the REST API backend using fetch
  const config = {
    method: method || "GET",
    body: data !== undefined ? JSON.stringify(data) : null,
    headers: {
      'content-type': 'application/json',
      'X-CSRFTOKEN': CSRF_TOKEN
    }
  };
  return fetch(endpoint, config)
           .then(handleResponse)
           .catch(onError)
}

let len = this.rowObject.length;
for (var i = 0; i <len; i++) {
  let onError = error => console.log("Error on rowObject " + i + ": " + error);
  apiService(endpoint, method, this.rowObject[i], onError);                   
}