mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-17 03:05:04 +08:00
Add support for ontimeout and onerror handler when using XMLHttpRequest for Android and iOS
Summary:Currently React-Native does not have `ontimeout` and `onerror` handlers for [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest). This is an extension to [No timeout on XMLHttpRequest](https://github.com/facebook/react-native/issues/4648). With addition to two handlers, both Android and iOS can now handle `ontimeout` if request times out and `onerror` when there is general network error. **Test plan** Code has been tested on both Android and iOS with [Charles](https://www.charlesproxy.com/) by setting a breakpoint on the request which fires `ontimeout` when the request waits beyond `timeout` time and `onerror` when there is network error. **Usage** JavaScript - ``` var request = new XMLHttpRequest(); function onLoad() { console.log(request.status); }; function onTimeout() { console.log('Timeout'); }; function onError() { console.log('General network error'); }; request.onload = onLoad; request.ontimeout = onTimeout; request.onerr Closes https://github.com/facebook/react-native/pull/6841 Differential Revision: D3178859 Pulled By: lexs fb-gh-sync-id: 30674570653e92ab5f7e74bd925dd5640fc862b6 fbshipit-source-id: 30674570653e92ab5f7e74bd925dd5640fc862b6
This commit is contained in:
committed by
Facebook Github Bot 9
parent
967dbd0cbe
commit
d09cd62011
@@ -122,6 +122,34 @@ request.open('GET', 'https://mywebsite.com/endpoint.php');
|
||||
request.send();
|
||||
```
|
||||
|
||||
You can also use -
|
||||
|
||||
```js
|
||||
var request = new XMLHttpRequest();
|
||||
|
||||
function onLoad() {
|
||||
console.log(request.status);
|
||||
console.log(request.responseText);
|
||||
};
|
||||
|
||||
function onTimeout() {
|
||||
console.log('Timeout');
|
||||
console.log(request.responseText);
|
||||
};
|
||||
|
||||
function onError() {
|
||||
console.log('General network error');
|
||||
console.log(request.responseText);
|
||||
};
|
||||
|
||||
request.onload = onLoad;
|
||||
request.ontimeout = onTimeout;
|
||||
request.onerror = onError;
|
||||
request.open('GET', 'https://mywebsite.com/endpoint.php');
|
||||
request.send();
|
||||
```
|
||||
|
||||
|
||||
Please follow the [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) for a complete description of the API.
|
||||
|
||||
As a developer, you're probably not going to use XMLHttpRequest directly as its API is very tedious to work with. But the fact that it is implemented and compatible with the browser API gives you the ability to use third-party libraries such as [frisbee](https://github.com/niftylettuce/frisbee) or [axios](https://github.com/mzabriskie/axios) directly from npm.
|
||||
|
||||
Reference in New Issue
Block a user