Axios now and is especially recommended to use, the official is not in the maintenance of Vue-reresource.
Because it is the first time to use the Axios, in the process of guessing a lot of pits
First we use VUE-CLI to create the project, the provider must be cross-domain, because our local service default address is generally localhost:8080 our server side is certainly not this, so the formation of cross-domain access, Axios does not support JSONP, So we're going to use http-proxy-middleware middleware as an agent,
Http-proxy-middleware's GitHub
Installation
--save-devnpm install --save-dev http-proxy-middleware// vue-cli 已经把http-proxy-middleware写在项目依赖里面了
Introduction of Axios
In the introduction of the project src/main.js Axios
‘axios‘Vue.prototype.$axios = axios;// axios 不支持Vue.use(axios)
Configuring the Http-proxy-middleware local agent
Open itconfig/index.js
var path =Require' Path ')Module.Exports = {Build: {Env:Require'./prod.env '),Index:path.resolve (__dirname,‘.. /dist/index.html '),AssetsRoot:path.resolve (__dirname,‘.. /dist '),Assetssubdirectory:' Static ',Assetspublicpath:‘./‘,Productionsourcemap:FalseGzipOffByDefault as many popular static hosts such asSurgeor netlify already gzip all static assetsFor you.Before setting to 'True ', make sureTo://NPM Install--save-dev Compression-webpack-pluginProductiongzip:FalseProductiongzipextensions: [' JS ',' CSS '],Run The Build commandWith a extra argument toView The Bundle Analyzer report after buildFinishes:// `NPM Run Build--report 'Set to 'True 'or 'False ' to always turn itOnOrOffBundleAnalyzerReport:process.env.npm_config_report},Dev: {Env:Require'./dev.env '),Port8080,Autoopenbrowser:TrueAssetssubdirectory:' Static ',Assetspublicpath:‘/‘,Proxytable: {Modify here Modify here},//CSS sourcemaps off by default because relative paths is "buggy" // Span class= "hljs-reserved" >with this option, according to the Css-loader README //(https://github.com/webpack/css-loader< Span class= "hljs-comment" > #sourcemaps) //in we experience, they generally work as expected, Span class= "Hljs-regexp" >//just be aware of this issue when enabling this option. csssourcemap: false}}
Here is the default configuration, find the proxytable modification inside the Dev object of the line surface
proxyTable: { ‘/api‘: { target:‘http://www.baidu.com/api‘, changeOrigin:true, pathRewrite:{ ‘^/api‘: ‘‘ } } }
targetThe parameter is the server address you want to access, you write in the code /api is equal to write this address, for example, I want to access http://www.baidu.com/api/login this interface in the code only to write /api/login it.
As far as build/dev.server.js there is no need to modify, there is already a package of methods.
// proxy api requestsObject.keys(proxyTable).forEach(function (context) { var options = proxyTable[context] if (typeof options === ‘string‘) { options = { target: options } } app.use(proxyMiddleware(options.filter || context, options))})
Online a lot of solutions are in the build/dev.server.js inside of the add content, completely no
Be sure to restart the service after you have finished the above operationctrl+cAnd thennpm run devBe sure to restart the service after you have finished the above operationctrl+cAnd thennpm run devBe sure to restart the service after you have finished the above operationctrl+cAnd thennpm run devAnd then we can use Axios to access the interface.
this.$axios({ method: "POST", withCredentials: false, url: "/api/login", data: { name: "1511328705UZVQ", psd: "123456" } }) .then(function(res) { console.log(res); }) .catch(function(err) { console.log(err); });
Handling cross-domain issues using Axios and Http-proxy-middleware agents