Axios Introduction
Axios is a promise-based HTTP library that can be used in browsers and node. js
Official Warehouse: Https://github.com/axios/axios
English Document: https://www.kancloud.cn/yunye/axios/234845
The use of Axios in the project (VUE)
There is no Vue project to use VUE-CLI scaffolding to generate a webpack template of the project can be a pleasant look down ~
If development encounters cross-domain issues, refer to: http://www.cnblogs.com/morang/p/8423763.html
Installing Axios into a project
npm install axios --save
Configure Wepack aliases to access different configuration interfaces in different environments
Configuration:
Use:import config from ‘config‘
Encapsulates a Axios instance
New fetch.js , creating Axios instances and filters here
If the agent is configured. Then config.apibaseurl configure the proxy prefix to
import config from ' config ' import axios from ' Axios ' //import Qs from ' QS '; const instance = axios . create ({ baseurl : config . apibaseurl //API Base_url timeout : 10000 //request time-out time //Transformrequest:data = qs.stringify (data) )
Axios The default submission format is: application/json , after the conversion is submitted in the formatapplication/x-www-form-urlencoded
in ASP. NET core, you need to add data to the action method [FromBody] that receives the JSON format, which is normal for application/x-www-form-urlencoded this modification.
qsyou can convert data format types by installing them into your project as you use them
npm install qs --save
Using the QS Conversion request comparison Chart
Adding interceptors to instances
//Add request blockerinstance.Interceptors.Request. Use(function(config){ //What to do before sending a request returnConfig; }, function(Error){ //What to do with the request error return Promise.Reject(Error); });//Add response blockerinstance.Interceptors.Response. Use(function(response){ //Do something about the response data returnResponse;}, function(Error){ //Do something about the response error return Promise.Reject(Error);}); //Last exposed instanceExport defaultInstance
Invocation of the instance
If express proxy is configured, the request path is: Browser->express Development Server-----Send request----> interface server
ImportFetch from ' Fetch.js '//getFetch({ URL:'/home/data ',//The full request path is configured for Fetch.js baseurl+/home/data?pageindex=1 Method:' GET ', params:{PageIndex:1}})//postFetch({ BaseURL:'/api/v1 ',//The full request path is/api/v1/home/save URL:'/home/save ', Method:' POST ', Data:{ID:1}})
Simple use of Axios in Vue project