README.md 5.4 KB
Newer Older
M
Matt Zabriskie 已提交
1
# axios [![Build Status](https://travis-ci.org/mzabriskie/axios.svg?branch=master)](https://travis-ci.org/mzabriskie/axios)
M
Matt Zabriskie 已提交
2

M
Matt Zabriskie 已提交
3 4 5 6 7 8 9 10 11
Promise based XHR library

## Features

- Making [XMLHttpRequests](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) supporting the [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) API
- Transforming request and response data
- Client side support for protecting against [XSRF](http://en.wikipedia.org/wiki/Cross-site_request_forgery)
- Specifying HTTP request headers
- Automatic transforms for JSON data
M
Matt Zabriskie 已提交
12

M
Matt Zabriskie 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
## Installing

Using bower:

```bash
$ bower install axios
```

Using npm:

```bash
$ npm install axios
```

## Compatability

Tested to work with >=IE8, Chrome, Firefox, Safari, and Opera.

M
Matt Zabriskie 已提交
31 32 33 34 35 36 37
## Example

Performing a `GET` request

```js
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
M
Matt Zabriskie 已提交
38 39 40
	.then(function (response) {
		console.log(response);
	})
M
Matt Zabriskie 已提交
41 42 43
	.catch(function (response) {
		console.log(response);
	});
M
Matt Zabriskie 已提交
44 45 46
	
// Optionally the request above could also be done as
axios.get('/user', {
M
Matt Zabriskie 已提交
47 48 49 50 51 52 53
		params: {
			ID: 12345
		}
	})
	.then(function (response) {
		console.log(response);
	})
M
Matt Zabriskie 已提交
54 55 56
	.catch(function (response) {
		console.log(response);
	});
M
Matt Zabriskie 已提交
57 58 59 60 61 62
```

Performing a `POST` request

```js
axios.post('/user', {
M
Matt Zabriskie 已提交
63 64 65 66 67 68
		firstName: 'Fred',
		lastName: 'Flintstone'
	})
	.then(function (response) {
		console.log(response);
	})
M
Matt Zabriskie 已提交
69 70 71
	.catch(function (response) {
		console.log(response);
	});
M
Matt Zabriskie 已提交
72 73 74 75 76 77
```

Aliases are provided for success and error

```js
axios.get('/user/12345')
M
Matt Zabriskie 已提交
78 79 80 81 82 83
	.success(function () {
		console.log('user found');
	})
	.error(function () {
		console.log('error finding user');
	});
M
Matt Zabriskie 已提交
84 85
```

M
Matt Zabriskie 已提交
86 87
Performing multiple concurrent requests

M
Matt Zabriskie 已提交
88
```js
M
Matt Zabriskie 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102
function getUserAccount() {
	return axios.get('/user/12345');
}

function getUserPermissions() {
	return axios.get('/user/permissions/12345');
}

axios.all([getUserAccount(), getUserPermissions()])
	.then(axios.spread(function (acct, perms) {
		// Both requests are now complete
	}));
```

M
Matt Zabriskie 已提交
103 104
## Request API

M
Matt Zabriskie 已提交
105
Requests can be made by passing the relevant config to `axios`.
M
Matt Zabriskie 已提交
106

M
Matt Zabriskie 已提交
107
##### axios(config)
M
Matt Zabriskie 已提交
108 109 110

```js
axios({
M
Matt Zabriskie 已提交
111 112
	method: 'get',
	url: '/user/12345'
M
Matt Zabriskie 已提交
113 114 115 116 117 118 119
});
```

### Request method aliases

For convenience aliases have been provided for all supported request methods.

M
Matt Zabriskie 已提交
120 121 122 123 124 125
##### axios.get(url[, config])
##### axios.delete(url[, config])
##### axios.head(url[, config])
##### axios.post(url[, data[, config]])
##### axios.put(url[, data[, config]])
##### axios.patch(url[, data[, config]])
M
Matt Zabriskie 已提交
126 127

###### NOTE
M
Matt Zabriskie 已提交
128
When using the alias methods `url`, `method`, and `data` properties don't need to be specified in config.
M
Matt Zabriskie 已提交
129

M
Matt Zabriskie 已提交
130 131 132 133 134
### Concurrency

##### axios.all(iterable)
##### axios.spread(callback)

M
Matt Zabriskie 已提交
135
### Config
M
Matt Zabriskie 已提交
136

M
Matt Zabriskie 已提交
137
This is the available config options for making requests. Only the `url` is required. Requests will default to `GET` if `method` is not specified.
M
Matt Zabriskie 已提交
138

M
Matt Zabriskie 已提交
139 140 141 142 143 144
```js
{
	// `url` is the server URL that will be used for the request
	url: '/user',
	
	// `method` is the request method to be used when making the request
M
Matt Zabriskie 已提交
145
	method: 'get', // default
M
Matt Zabriskie 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	
	// `transformRequest` allows changes to the request data before it is sent to the server
	// This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
	transformRequest: [function (data) {
		// Do whatever you want to transform the data
		
		return data;
	}],
	
	// `transformResponse` allows changes to the response data to be made before
	// it is passed to the success/error handlers
	transformResponse: [function (data) {
		// Do whatever you want to transform the data
		
		return data;
	}],
	
	// `headers` are custom headers to be sent
	headers: {'X-Requested-With': 'XMLHttpRequest'},
	
	// `param` are the URL parameters to be sent with the request
	params: {
		ID: 12345
	},
	
	// `data` is the data to be sent as the request body
	// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
	data: {
		firstName: 'Fred'
	},
	
M
Matt Zabriskie 已提交
177 178
	// `withCredentials` indicates whether or not cross-site Access-Control requests
	// should be made using credentials
M
Matt Zabriskie 已提交
179
	withCredentials: false, // default
M
Matt Zabriskie 已提交
180
	
M
Matt Zabriskie 已提交
181 182
	// `responseType` indicates the type of data that the server will respond with
	// options are 'arraybuffer', 'blob', 'document', 'json', 'text'
M
Matt Zabriskie 已提交
183 184 185 186 187 188 189
	responseType: 'json', // default
	
	// `xsrfCookieName` is the name of the cookie to use as a value for xsrf token
	xsrfCookieName: 'XSRF-TOKEN', // default
	
	// `xsrfHeaderName` is the name of the http header that carries the xsrf token value
	xsrfHeaderName: 'X-XSRF-TOKEN' // default
M
Matt Zabriskie 已提交
190 191 192 193 194 195 196
}
```

## Response API

For either `success` or `error`, the following response will be provided.

M
Matt Zabriskie 已提交
197
```js
M
Matt Zabriskie 已提交
198 199 200 201 202 203 204 205 206 207 208
axios.get('/user/12345')
	.success(function (
		// `data` is the response that was provided by the server
		data,
		
		// `status` is the HTTP status code from the server response
		status,
		
		// `headers` the headers that the server responded with
		headers,
		
M
Matt Zabriskie 已提交
209
		// `config` is the config that was provided to `axios` for the request
M
Matt Zabriskie 已提交
210 211 212 213
		config
		) {
		// Do something with result
	});
M
Matt Zabriskie 已提交
214 215 216
}
```

M
Matt Zabriskie 已提交
217
## Credits
M
Matt Zabriskie 已提交
218

M
Matt Zabriskie 已提交
219 220 221
axios is heavily inspired by the [$http service](https://docs.angularjs.org/api/ng/service/$http) provided in [Angular](https://angularjs.org/). Ultimately axios is an effort to provide a standalone `$http`-like service for use outside of Angular.

axios uses the [es6-promise](https://github.com/jakearchibald/es6-promise) polyfill by [Jake Archibald](https://github.com/jakearchibald). Until we [can use](http://caniuse.com/promises) ES6 Promises natively in all browsers, this polyfill is a life saver.
M
Matt Zabriskie 已提交
222 223 224 225

## License

MIT