README.md 3.2 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 3

Lightweight Promise based XHR library
M
Matt Zabriskie 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

## Example

Performing a `GET` request

```js
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
	.success(function (response) {
		console.log(response);
	});
	
// Optionally the request above could also be done as
axios.get('/user', {
	params: {
		ID: 12345
	}
})
.success(function (response) {
	console.log(response);
});
```

Performing a `POST` request

```js
axios.post('/user', {
	firstName: 'Fred',
	lastName: 'Flintstone'
})
.success(function (response) {
	console.log(response);
});
```

## Request API

Requests can be made by passing the relevant options to `axios`.

##### axios(options)

```js
axios({
	url: '/user/12345',
	method: 'get'
});
```

### Request method aliases

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

##### axios.get(url[, options])
##### axios.delete(url[, options])
##### axios.head(url[, options])
##### axios.post(url[, data[, options]])
##### axios.put(url[, data[, options]])
##### axios.patch(url[, data[, options]])

###### NOTE
When using the alias methods `url`, `method`, and `data` properties don't need to be specified in options.

### Options

```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
	method: 'get',
	
	// `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 已提交
106 107
	// `withCredentials` indicates whether or not cross-site Access-Control requests
	// should be made using credentials
M
Matt Zabriskie 已提交
108 109
	withCredentials: true,
	
M
Matt Zabriskie 已提交
110 111 112
	// `responseType` indicates the type of data that the server will responsd with
	// options are 'arraybuffer', 'blob', 'document', 'json', 'text'
	responseType: 'json'
M
Matt Zabriskie 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
}
```

## Response API

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

```js
{
	// `data` is the response that was provided by the server
	data: {/*...*/}	,
	
	// `status` is the HTTP status code from the server response
	status: 200,
	
	// `headers` the headers that the server responded with
	headers: {/*...*/},
	
	// `config` are the options that were provided to `axios` for the request
	config: {/*...*/}
}
```

## Installing

Using bower:

```bash
$ bower install axios
```

Using npm:

```bash
$ npm install axios
```

## Attribution

axios is heavily inspired by Angular's $http service.

## License

MIT