First, Introduction
Look at the profile of the official website:
"Promise based HTTP client for the browser and node. js"
HTTP request client based on Promise, which can be used in both the browser and node. js.
Second, features:
1, send xmlhttprequests request in the browser;
2. Send HTTP requests in node. js;
3, support Promise API;
4, interception request and response;
5, conversion request and response data;
6. Automatic conversion of JSON data;
7, the client supports the protection of security from XSRF attacks;
Third, installation (official website)
Iv. Application
1. Send a GET request
Axios.get ('/welfare ', {
Params: {
Giftpackid:1
}
})
. Then (function (res) {
Console.log (RES);
})
. catch (function (res) {
Console.log (RES);
});
2. Send a POST request
123456789 |
axios.post(
‘/welfare‘
, {
giftPackId: 1
})
.then(
function (res) {
console.log(res);
})
.
catch
(
function
(res) {
console.log(res);
});
|
3. Send multiple concurrent Requests
123456789101112 |
function
getUserAccount() {
return
axios.get(
‘/welfare‘
);
}
function
getUserPermissions() {
return
axios.get(
‘/getWelfare‘
);
}
axios.all([getUserAccount(), getUserPermissions()])
.then(axios.spread(
function
(acct, perms) {
// ok
}));
|
4, in addition to the Axios also provides the following several request methods:
12345678910111213 |
axios.request(config) 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]]) |
5. Compatibility processing
The project found that phones in Android 4.3 and below do not support the use of Axios, mainly the inability to use promise. Add the following polyfill to it.
Installing Es6-promise in the project
1 |
cnpm install es6-promise --save-dev |
At the beginning of axios.min.js, add
1 |
require( ‘es6-promise‘ ).polyfill(); |
Ok!
Compatibility of Axios