Switched recommendation from superagent to frisbee, Moved XMLHTTPRequest to bottom of doc page

Resolves issues per recommendations by @ide

Added axios reference
This commit is contained in:
Nick Baugh 2015-12-03 01:03:36 -05:00
parent b0e39d26ae
commit 7605d2d8cd
1 changed files with 32 additions and 32 deletions

View File

@ -9,45 +9,19 @@ next: timers
One of React Native's goals is to be a playground where we can experiment with different architectures and crazy ideas. Since browsers are not flexible enough, we had no choice but to reimplement the entire stack. In the places that we did not intend to change anything, we tried to be as faithful as possible to the browser APIs. The networking stack is a great example.
## XMLHttpRequest
XMLHttpRequest API is implemented on-top of [iOS networking apis](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html). The notable difference from web is the security model: you can read from arbitrary websites on the internet since there is no concept of [CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing).
```javascript
var request = new XMLHttpRequest();
request.onreadystatechange = (e) => {
if (request.readyState !== 4) {
return;
}
if (request.status === 200) {
console.log('success', request.responseText);
} else {
console.warn('error');
}
};
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 [Parse]( https://parse.com/products/javascript) or [super-agent](https://github.com/visionmedia/superagent) directly from npm.
## Fetch
[fetch](https://fetch.spec.whatwg.org/) is a better networking API being worked on by the standards committee and is already available in Chrome. It is available in React Native by default.
#### Usage
```javascript
```js
fetch('https://mywebsite.com/endpoint/')
```
Include a request object as the optional second argument to customize the HTTP request:
```javascript
```js
fetch('https://mywebsite.com/endpoint/', {
method: 'POST',
headers: {
@ -62,11 +36,12 @@ fetch('https://mywebsite.com/endpoint/', {
```
#### Async
`fetch` returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that can be processed in two ways:
1. Using `then` and `catch` in synchronous code:
```javascript
```js
fetch('https://mywebsite.com/endpoint.php')
.then((response) => response.text())
.then((responseText) => {
@ -79,7 +54,7 @@ fetch('https://mywebsite.com/endpoint.php')
2. Called within an asynchronous function using ES7 `async`/`await` syntax:
```javascript
```js
async getUsersFromApi() {
try {
let response = await fetch('https://mywebsite.com/endpoint/');
@ -92,12 +67,11 @@ async getUsersFromApi() {
- Note: Errors thrown by rejected Promises need to be caught, or they will be swallowed silently
## WebSocket
WebSocket is a protocol providing full-duplex communication channels over a single TCP connection.
```javascript
```js
var ws = new WebSocket('ws://host.com/path');
ws.on('open', function() {
@ -120,3 +94,29 @@ ws.on('close', function(e) {
console.log(e.code, e.reason);
});
```
## XMLHttpRequest
XMLHttpRequest API is implemented on-top of [iOS networking apis](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html). The notable difference from web is the security model: you can read from arbitrary websites on the internet since there is no concept of [CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing).
```js
var request = new XMLHttpRequest();
request.onreadystatechange = (e) => {
if (request.readyState !== 4) {
return;
}
if (request.status === 200) {
console.log('success', request.responseText);
} else {
console.warn('error');
}
};
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 [Parse](https://parse.com/products/javascript), [frisbee](https://github.com/niftylettuce/frisbee), or [axios](https://github.com/mzabriskie/axios) directly from npm.