【问题标题】:React-Native Fetch/Axios no statusTextReact-Native Fetch/Axios 没有 statusText
【发布时间】:2018-12-06 01:07:54
【问题描述】:

我对使用 fetch 或 axios 库的 react-native ajax 调用有一些问题。无法在失败的 Ajax 调用上获取 http statusText。 statusText 属性始终未定义。

这里有一个简单的例子:

  axios.post(this._apiBasePath + url, data, {
            headers: this._header,
            timeout:5000,
            responseType: respType
        }).then(d => {
            res(d.data);
        }).catch(err => {
            if(err.response){
               console.log(err.response);
               // there is a status 401 but no statusText in err Object
            }
        });

也许你们可以帮助我?

json of the error object

(在 iOS 和 Android 模拟器中测试)

(反应 16.4.1 / 反应原生 0.55.4)

【问题讨论】:

  • 用axios,相信可能是err.data.response?
  • 数据在 err 对象中为空。我认为它只是在成功案例中填写。
  • err.response.data / err.response.headers / err.response.status?
  • 我更新了问题并添加了带有 console.log 输出的屏幕截图
  • 如果我直接从浏览器(相同的后端)执行相同的请求,它会返回以下内容(chrome 调试器):状态代码:401 [] 无法验证用户“测试”!原因:未找到用户“asd”。所以有一个返回的statusText

标签: react-native fetch axios


【解决方案1】:

你可以试试下面的代码,安卓可以调用http,IOS只能调用https

   constructor(props){
        super(props);
        this.state={
            lists:[]
        }
    }
    componentDidMount(){
        return fetch(url, {method: "GET"})
        .then((response) => response.json())
        .then((responseData) => {
           this.setState({ lists: responseData.result });
        })
        .done();
    }

【讨论】:

  • 我从后端获取数据没有问题。同样使用http(在模拟器中)我的问题是,如果呼叫不起作用(可能是错误的凭据或其他原因),我无法从后端读出 statusText 以向用户显示一些信息。 (因为 statusText 总是未定义的)
猜你喜欢
  • 2021-08-07
  • 2021-03-14
  • 2021-05-20
  • 1970-01-01
  • 2019-07-09
  • 2020-01-26
  • 2020-05-01
  • 1970-01-01
  • 2019-10-18
相关资源
最近更新 更多