简体   繁体   English

为什么 axios 不处理 'Content-Type': 'application/json'

[英]Why axios do not handle with 'Content-Type': 'application/json'

I'm making requests with Axios and everything works correctly, but if there is an error in the PHP API like: "Undefined index:...".我正在向 Axios 发出请求,并且一切正常,但是如果 PHP API 中有错误,例如:“未定义的索引:...”。 Axios doesn't handle the error with 'Content-Type': 'application/json' Axios 无法处理“Content-Type”错误:“application/json”

const API = axios.create({
  baseURL: URL + "/api/",
  headers: {
    'Content-Type': 'application/json',
  }
})
            let data = {
              orderList: {
                limit: 10,
                page: page,
                orderby: orderBy,
                search: search
              }
            };
            data = qs.stringify(data);
            let orderList;
            await API.post("api.php", data, {
              headers: {
                'Content-Type': 'application/json',
              }
            }).then(response => {
              orderList = response.data;
            }).catch(() => {
              orderList = 'Network Error';
            })
            return orderList;
          },

I wanted it to return only the JSON result from the API, if not, trigger catch.我希望它只返回来自 API 的 JSON 结果,如果没有,则触发捕获。 However catch is not being triggered.但是 catch 没有被触发。

use header like this像这样使用 header

axios.defaults.headers.post['Content-Type'] = 'application/json';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM