【问题标题】:Axios: unexpected end of streamAxios:流的意外结束
【发布时间】:2019-06-19 02:44:27
【问题描述】:

我在 react native 中使用 axios 获取大量数据(JSON)时收到以下错误。

错误:流意外结束

这是我的代码:

axios.post('http://192.168.0.1:5000/download', {
      access: data.tokens
    })
    .then(function (response) {
       alert(response.data);
    })
    .catch(function (error) {
      alert("There was an error in communicating to server");
});

我试图限制 REST API 返回的结果(100 行),它返回了一些数据。

如何在 Axios 中获取大量数据?

【问题讨论】:

  • 标题:{“连接”:“关闭”}
  • 我试过了,但没用。出现同样的错误
  • 也发布您的错误。
  • axios 只返回错误:流的意外结束

标签: react-native axios


【解决方案1】:

原来axios请求的默认超时设置为0。我通过在axios配置中添加timeout解决了这个问题。

axios({
      method: 'post',
      timeout: 1000,
      url: 'http://192.168.0.1:5000/download',
      data: {
        access: data.token
      }
    })
.then(function (response) {
   alert(response.data);
})
.catch(function (error) {
  alert("There was an error in communicating to server");
});

由于某种原因,timeout 参数放在data 参数之后将不起作用

【讨论】:

  • 嗨@pikanerd,赞成,我怀疑我正在为axios使用内联较短的语法:axios.post('url' + data)。如何在这里添加超时?请告诉。
  • 可能是这样的? axios.post('sample-site', {data: id: '1'}, timeout: 1000)
猜你喜欢
  • 2015-11-17
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多