Preface: vue-resource implements asynchronous request function in a more concise way than jQuery, and also provides functions such as interceptors to process behavior during requests. The following introduces the commonly used GET and POST request usage and encapsulation methods in vue-resource.
accessGithub Get the latest development files and documents
feature:
- Supports Promise API and URI Templates
- Support the use of interceptors during requesting
- Supports Firefox, Chrome, Safari, Opera and IE9+
- Very small (14KB after compression, only 5.3KB after gzipped)
Parameter description:
Parameter descriptions have been mentioned in many articles. Only the necessary parameters are used here. For details, please visit Document in Github
GET Request
function getRequest(url, params) { return new Promise((resolve, reject) => { ( url, { params: params }, {emulateJSON: true} ) .then((res) => { resolve(res); }) .catch((res) => { reject(res); }); }); }
POST request
function postRequest(url, params) { return new Promise((resolve, reject) => { ( url, { params }, {emulateJSON: true} ) .then((res) => { resolve(); }) .catch((res) => { reject(); }); }); }
How to use
var params = new Object(); //Create params objectvar = id; //Pass parametersvar url = url; //url addresspostRequest(url, params) .then((message) => { //The callback is successfully processed here}) .catch((message) => { //The failed callback is processed here});
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.