Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么axios不处理“内容类型”:“应用程序/json”_Php_React Native_Axios - Fatal编程技术网

Php 为什么axios不处理“内容类型”:“应用程序/json”

Php 为什么axios不处理“内容类型”:“应用程序/json”,php,react-native,axios,Php,React Native,Axios,我正在使用Axios发出请求,并且一切正常,但是如果PHP API中出现错误,比如:未定义索引:。。。。Axios不使用“内容类型”:“应用程序/json”处理错误 const API = axios.create({ baseURL: URL + "/api/", headers: { 'Content-Type': 'application/json', } }) let data = { order

我正在使用Axios发出请求,并且一切正常,但是如果PHP API中出现错误,比如:未定义索引:。。。。Axios不使用“内容类型”:“应用程序/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;
          },
我希望它只从API返回JSON结果,如果不是,则返回触发器捕获。但是,不会触发捕获。

使用如下标题

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

标题:{'Accept':'application/json',}不起作用@lawrencecherone返回的响应的HTTP状态码是什么?200? 500?@ceejayoz 200。细节:我触发了PHP中的错误,我在终端反应本地日志中看到了它,这就是为什么。200状态不会触发捕获。这不是一个错误;Axios会认为这是成功的。如果您将错误的JSON数据作为200个响应返回,则必须在then块中处理它们。请在回答问题时提供一些解释。