React-native IOS,TypeError:网络请求失败

时间:2016-03-19 10:43:36

标签: react-native fetch

我正在尝试使用FETCH在react-native中发布请求。但是,请求失败有时会抛出以下消息:TypeError:网络请求失败。 我用来发送POST请求的代码:

fetch(query,{
      method: 'POST',
      headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/x-www-form-urlencoded'
                },
      body: JSON.stringify(data)
      })
      .then(Utils.checkStatus)
      .then(Utils.parseJSON)
      .then(json => this._handleResponseComments(json))
      .catch(error => {

         this.setState({isLoading:false, error: 'Error!'});

      });

我的代码出了什么问题?

编辑:

在记录xhr对象时发现了一些线索:

{ UNSENT: 0,
  OPENED: 1,
  HEADERS_RECEIVED: 2,
  LOADING: 3,
  DONE: 4,
  onreadystatechange: null,
  onload: [Function],
  upload: {},
  readyState: 4,
  responseHeaders: undefined,
  responseText: 'The network connection was lost.',
  status: 0,

1 个答案:

答案 0 :(得分:0)

好的,我终于找到了问题的根源。在Apache上的虚拟主机配置上增加KeepAliveTimeout解决了这个问题。只需将KeepAliveTimeout 30设置为我工作。

KeepAlive On KeepAliveTimeout 30 MaxKeepAliveRequests 0

相关问题