Vue 代理服务器___Vue 跨域通过Axios 的ajax方式的get请求获取数据
Vue 脚手架代理配置说明
## vue脚手架配置代理 ### 方法一 在vue.config.js中添加如下配置: ```js devServer:{ proxy:"http://localhost:5000" } ``` 说明: 1. 优点:配置简单,请求资源时直接发给前端(8080)即可。 2. 缺点:不能配置多个代理,不能灵活的控制请求是否走代理。 3. 工作方式:若按照上述配置代理,当请求了前端不存在的资源时,那么该请求会转发给服务器 (优先匹配前端资源) ### 方法二 编写vue.config.js配置具体代理规则: ```js module.exports = { devServer: { proxy: { '/api1': {// 匹配所有以 '/api1'开头的请求路径 target: 'http://localhost:5000',// 代理目标的基础路径 changeOrigin: true, pathRewrite: {'^/api1': ''} }, '/api2': {// 匹配所有以 '/api2'开头的请求路径 target: 'http://localhost:5001',// 代理目标的基础路径 changeOrigin: true, pathRewrite: {'^/api2': ''} } } } } /* changeOrigin设置为true时,服务器收到的请求头中的host为:localhost:5000 changeOrigin设置为false时,服务器收到的请求头中的host为:localhost:8080 changeOrigin默认值为true */ ``` 说明: 1. 优点:可以配置多个代理,且可以灵活的控制请求是否走代理。 2. 缺点:配置略微繁琐,请求资源时必须加前缀。
Vue 代理服务器___Vue 跨域通过Axios 的ajax方式的get请求获取数据
1、说明
1.1:配置2台本地服务器
说明:node_modules 为 vue 脚手架
package.json 为静态数据
server1.js 为模拟服务器1
server2.js 为模拟服务器2
yarn.lock 为配置文件
1.1.1:package.json
{ "name": "server", "version": "1.0.0", "description": "", "main": "index.js", "keywords": [], "author": "", "license": "ISC", "dependencies": { "express": "^4.17.1" } }
1.1.2 server1
const express = require('express') const app = express() app.use((request,response,next)=>{ console.log('有人请求服务器1了'); // console.log('请求来自于',request.get('Host')); // console.log('请求的地址',request.url); next() }) app.get('/students',(request,response)=>{ const students = [ {id:'001',name:'tom',age:18}, {id:'002',name:'jerry',age:19}, {id:'003',name:'tony',age:120}, ] response.send(students) }) app.listen(5000,(err)=>{ if(!err) console.log('服务器1启动成功了,请求学生信息地址为:http://localhost:5000/students'); })
1.1.3 server2
const express = require('express') const app = express() app.use((request,response,next)=>{ console.log('有人请求服务器2了'); next() }) app.get('/cars',(request,response)=>{ const cars = [ {id:'001',name:'奔驰',price:199}, {id:'002',name:'马自达',price:109}, {id:'003',name:'捷达',price:120}, ] response.send(cars) }) app.listen(5001,(err)=>{ if(!err) console.log('服务器2启动成功了,请求汽车信息地址为:http://localhost:5001/cars'); })
1.1.4 yarn.lock
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. # yarn lockfile v1 accepts@~1.3.7: version "1.3.7" resolved "https://registry.npm.taobao.org/accepts/download/accepts-1.3.7.tgz#531bc726517a3b2b41f850021c6cc15eaab507cd" integrity sha1-UxvHJlF6OytB+FACHGzBXqq1B80= dependencies: mime-types "~2.1.24" negotiator "0.6.2" array-flatten@1.1.1: version "1.1.1" resolved "https://registry.npm.taobao.org/array-flatten/download/array-flatten-1.1.1.tgz?cache=0&sync_timestamp=1574313293899&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Farray-flatten%2Fdownload%2Farray-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2" integrity sha1-ml9pkFGx5wczKPKgCJaLZOopVdI= body-parser@1.19.0: version "1.19.0" resolved "https://registry.npm.taobao.org/body-parser/download/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a" integrity sha1-lrJwnlfJxOCab9Zqj9l5hE9p8Io= dependencies: bytes "3.1.0" content-type "~1.0.4" debug "2.6.9" depd "~1.1.2" http-errors "1.7.2" iconv-lite "0.4.24" on-finished "~2.3.0" qs "6.7.0" raw-body "2.4.0" type-is "~1.6.17" bytes@3.1.0: version "3.1.0" resolved "https://registry.npm.taobao.org/bytes/download/bytes-3.1.0.tgz#f6cf7933a360e0588fa9fde85651cdc7f805d1f6" integrity sha1-9s95M6Ng4FiPqf3oVlHNx/gF0fY= content-disposition@0.5.3: version "0.5.3" resolved "https://registry.npm.taobao.org/content-disposition/download/content-disposition-0.5.3.tgz#e130caf7e7279087c5616c2007d0485698984fbd" integrity sha1-4TDK9+cnkIfFYWwgB9BIVpiYT70= dependencies: safe-buffer "5.1.2" content-type@~1.0.4: version "1.0.4" resolved "https://registry.npm.taobao.org/content-type/download/content-type-1.0.4.tgz#e138cc75e040c727b1966fe5e5f8c9aee256fe3b" integrity sha1-4TjMdeBAxyexlm/l5fjJruJW/js= cookie-signature@1.0.6: version "1.0.6" resolved "https://registry.npm.taobao.org/cookie-signature/download/cookie-signature-1.0.6.tgz#e303a882b342cc3ee8ca513a79999734dab3ae2c" integrity sha1-4wOogrNCzD7oylE6eZmXNNqzriw= cookie@0.4.0: version "0.4.0" resolved "https://registry.npm.taobao.org/cookie/download/cookie-0.4.0.tgz?cache=0&sync_timestamp=1587525998658&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcookie%2Fdownload%2Fcookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba" integrity sha1-vrQ35wIrO21JAZ0IhmUwPr6cFLo= debug@2.6.9: version "2.6.9" resolved "https://registry.npm.taobao.org/debug/download/debug-2.6.9.tgz?cache=0&sync_timestamp=1600502855452&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fdebug%2Fdownload%2Fdebug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" integrity sha1-XRKFFd8TT/Mn6QpMk/Tgd6U2NB8= dependencies: ms "2.0.0" depd@~1.1.2: version "1.1.2" resolved "https://registry.npm.taobao.org/depd/download/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9" integrity sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak= destroy@~1.0.4: version "1.0.4" resolved "https://registry.npm.taobao.org/destroy/download/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80" integrity sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA= ee-first@1.1.1: version "1.1.1" resolved "https://registry.npm.taobao.org/ee-first/download/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d" integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0= encodeurl@~1.0.2: version "1.0.2" resolved "https://registry.npm.taobao.org/encodeurl/download/encodeurl-1.0.2.tgz#ad3ff4c86ec2d029322f5a02c3a9a606c95b3f59" integrity sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k= escape-html@~1.0.3: version "1.0.3" resolved "https://registry.npm.taobao.org/escape-html/download/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988" integrity sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg= etag@~1.8.1: version "1.8.1" resolved "https://registry.npm.taobao.org/etag/download/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887" integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc= express@^4.17.1: version "4.17.1" resolved "https://registry.npm.taobao.org/express/download/express-4.17.1.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fexpress%2Fdownload%2Fexpress-4.17.1.tgz#4491fc38605cf51f8629d39c2b5d026f98a4c134" integrity sha1-RJH8OGBc9R+GKdOcK10Cb5ikwTQ= dependencies: accepts "~1.3.7" array-flatten "1.1.1" body-parser "1.19.0" content-disposition "0.5.3" content-type "~1.0.4" cookie "0.4.0" cookie-signature "1.0.6" debug "2.6.9" depd "~1.1.2" encodeurl "~1.0.2" escape-html "~1.0.3" etag "~1.8.1" finalhandler "~1.1.2" fresh "0.5.2" merge-descriptors "1.0.1" methods "~1.1.2" on-finished "~2.3.0" parseurl "~1.3.3" path-to-regexp "0.1.7" proxy-addr "~2.0.5" qs "6.7.0" range-parser "~1.2.1" safe-buffer "5.1.2" send "0.17.1" serve-static "1.14.1" setprototypeof "1.1.1" statuses "~1.5.0" type-is "~1.6.18" utils-merge "1.0.1" vary "~1.1.2" finalhandler@~1.1.2: version "1.1.2" resolved "https://registry.npm.taobao.org/finalhandler/download/finalhandler-1.1.2.tgz#b7e7d000ffd11938d0fdb053506f6ebabe9f587d" integrity sha1-t+fQAP/RGTjQ/bBTUG9uur6fWH0= dependencies: debug "2.6.9" encodeurl "~1.0.2" escape-html "~1.0.3" on-finished "~2.3.0" parseurl "~1.3.3" statuses "~1.5.0" unpipe "~1.0.0" forwarded@~0.1.2: version "0.1.2" resolved "https://registry.npm.taobao.org/forwarded/download/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84" integrity sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ= fresh@0.5.2: version "0.5.2" resolved "https://registry.npm.taobao.org/fresh/download/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7" integrity sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac= http-errors@1.7.2: version "1.7.2" resolved "https://registry.npm.taobao.org/http-errors/download/http-errors-1.7.2.tgz#4f5029cf13239f31036e5b2e55292bcfbcc85c8f" integrity sha1-T1ApzxMjnzEDblsuVSkrz7zIXI8= dependencies: depd "~1.1.2" inherits "2.0.3" setprototypeof "1.1.1" statuses ">= 1.5.0 < 2" toidentifier "1.0.0" http-errors@~1.7.2: version "1.7.3" resolved "https://registry.npm.taobao.org/http-errors/download/http-errors-1.7.3.tgz#6c619e4f9c60308c38519498c14fbb10aacebb06" integrity sha1-bGGeT5xgMIw4UZSYwU+7EKrOuwY= dependencies: depd "~1.1.2" inherits "2.0.4" setprototypeof "1.1.1" statuses ">= 1.5.0 < 2" toidentifier "1.0.0" iconv-lite@0.4.24: version "0.4.24" resolved "https://registry.npm.taobao.org/iconv-lite/download/iconv-lite-0.4.24.tgz?cache=0&sync_timestamp=1594184297399&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Ficonv-lite%2Fdownload%2Ficonv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b" integrity sha1-ICK0sl+93CHS9SSXSkdKr+czkIs= dependencies: safer-buffer ">= 2.1.2 < 3" inherits@2.0.3: version "2.0.3" resolved "https://registry.npm.taobao.org/inherits/download/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4= inherits@2.0.4: version "2.0.4" resolved "https://registry.npm.taobao.org/inherits/download/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" integrity sha1-D6LGT5MpF8NDOg3tVTY6rjdBa3w= ipaddr.js@1.9.1: version "1.9.1" resolved "https://registry.npm.taobao.org/ipaddr.js/download/ipaddr.js-1.9.1.tgz#bff38543eeb8984825079ff3a2a8e6cbd46781b3" integrity sha1-v/OFQ+64mEglB5/zoqjmy9RngbM= media-typer@0.3.0: version "0.3.0" resolved "https://registry.npm.taobao.org/media-typer/download/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g= merge-descriptors@1.0.1: version "1.0.1" resolved "https://registry.npm.taobao.org/merge-descriptors/download/merge-descriptors-1.0.1.tgz#b00aaa556dd8b44568150ec9d1b953f3f90cbb61" integrity sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E= methods@~1.1.2: version "1.1.2" resolved "https://registry.npm.taobao.org/methods/download/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee" integrity sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4= mime-db@1.44.0: version "1.44.0" resolved "https://registry.npm.taobao.org/mime-db/download/mime-db-1.44.0.tgz?cache=0&sync_timestamp=1600831152656&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fmime-db%2Fdownload%2Fmime-db-1.44.0.tgz#fa11c5eb0aca1334b4233cb4d52f10c5a6272f92" integrity sha1-+hHF6wrKEzS0Izy01S8QxaYnL5I= mime-types@~2.1.24: version "2.1.27" resolved "https://registry.npm.taobao.org/mime-types/download/mime-types-2.1.27.tgz#47949f98e279ea53119f5722e0f34e529bec009f" integrity sha1-R5SfmOJ56lMRn1ci4PNOUpvsAJ8= dependencies: mime-db "1.44.0" mime@1.6.0: version "1.6.0" resolved "https://registry.npm.taobao.org/mime/download/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1" integrity sha1-Ms2eXGRVO9WNGaVor0Uqz/BJgbE= ms@2.0.0: version "2.0.0" resolved "https://registry.npm.taobao.org/ms/download/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g= ms@2.1.1: version "2.1.1" resolved "https://registry.npm.taobao.org/ms/download/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a" integrity sha1-MKWGTrPrsKZvLr5tcnrwagnYbgo= negotiator@0.6.2: version "0.6.2" resolved "https://registry.npm.taobao.org/negotiator/download/negotiator-0.6.2.tgz#feacf7ccf525a77ae9634436a64883ffeca346fb" integrity sha1-/qz3zPUlp3rpY0Q2pkiD/+yjRvs= on-finished@~2.3.0: version "2.3.0" resolved "https://registry.npm.taobao.org/on-finished/download/on-finished-2.3.0.tgz#20f1336481b083cd75337992a16971aa2d906947" integrity sha1-IPEzZIGwg811M3mSoWlxqi2QaUc= dependencies: ee-first "1.1.1" parseurl@~1.3.3: version "1.3.3" resolved "https://registry.npm.taobao.org/parseurl/download/parseurl-1.3.3.tgz#9da19e7bee8d12dff0513ed5b76957793bc2e8d4" integrity sha1-naGee+6NEt/wUT7Vt2lXeTvC6NQ= path-to-regexp@0.1.7: version "0.1.7" resolved "https://registry.npm.taobao.org/path-to-regexp/download/path-to-regexp-0.1.7.tgz?cache=0&sync_timestamp=1601401033318&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fpath-to-regexp%2Fdownload%2Fpath-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c" integrity sha1-32BBeABfUi8V60SQ5yR6G/qmf4w= proxy-addr@~2.0.5: version "2.0.6" resolved "https://registry.npm.taobao.org/proxy-addr/download/proxy-addr-2.0.6.tgz#fdc2336505447d3f2f2c638ed272caf614bbb2bf" integrity sha1-/cIzZQVEfT8vLGOO0nLK9hS7sr8= dependencies: forwarded "~0.1.2" ipaddr.js "1.9.1" qs@6.7.0: version "6.7.0" resolved "https://registry.npm.taobao.org/qs/download/qs-6.7.0.tgz?cache=0&sync_timestamp=1588543068614&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fqs%2Fdownload%2Fqs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc" integrity sha1-QdwaAV49WB8WIXdr4xr7KHapsbw= range-parser@~1.2.1: version "1.2.1" resolved "https://registry.npm.taobao.org/range-parser/download/range-parser-1.2.1.tgz#3cf37023d199e1c24d1a55b84800c2f3e6468031" integrity sha1-PPNwI9GZ4cJNGlW4SADC8+ZGgDE= raw-body@2.4.0: version "2.4.0" resolved "https://registry.npm.taobao.org/raw-body/download/raw-body-2.4.0.tgz#a1ce6fb9c9bc356ca52e89256ab59059e13d0332" integrity sha1-oc5vucm8NWylLoklarWQWeE9AzI= dependencies: bytes "3.1.0" http-errors "1.7.2" iconv-lite "0.4.24" unpipe "1.0.0" safe-buffer@5.1.2: version "5.1.2" resolved "https://registry.npm.taobao.org/safe-buffer/download/safe-buffer-5.1.2.tgz?cache=0&sync_timestamp=1589129611964&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fsafe-buffer%2Fdownload%2Fsafe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" integrity sha1-mR7GnSluAxN0fVm9/St0XDX4go0= "safer-buffer@>= 2.1.2 < 3": version "2.1.2" resolved "https://registry.npm.taobao.org/safer-buffer/download/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" integrity sha1-RPoWGwGHuVSd2Eu5GAL5vYOFzWo= send@0.17.1: version "0.17.1" resolved "https://registry.npm.taobao.org/send/download/send-0.17.1.tgz#c1d8b059f7900f7466dd4938bdc44e11ddb376c8" integrity sha1-wdiwWfeQD3Rm3Uk4vcROEd2zdsg= dependencies: debug "2.6.9" depd "~1.1.2" destroy "~1.0.4" encodeurl "~1.0.2" escape-html "~1.0.3" etag "~1.8.1" fresh "0.5.2" http-errors "~1.7.2" mime "1.6.0" ms "2.1.1" on-finished "~2.3.0" range-parser "~1.2.1" statuses "~1.5.0" serve-static@1.14.1: version "1.14.1" resolved "https://registry.npm.taobao.org/serve-static/download/serve-static-1.14.1.tgz#666e636dc4f010f7ef29970a88a674320898b2f9" integrity sha1-Zm5jbcTwEPfvKZcKiKZ0MgiYsvk= dependencies: encodeurl "~1.0.2" escape-html "~1.0.3" parseurl "~1.3.3" send "0.17.1" setprototypeof@1.1.1: version "1.1.1" resolved "https://registry.npm.taobao.org/setprototypeof/download/setprototypeof-1.1.1.tgz#7e95acb24aa92f5885e0abef5ba131330d4ae683" integrity sha1-fpWsskqpL1iF4KvvW6ExMw1K5oM= "statuses@>= 1.5.0 < 2", statuses@~1.5.0: version "1.5.0" resolved "https://registry.npm.taobao.org/statuses/download/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c" integrity sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow= toidentifier@1.0.0: version "1.0.0" resolved "https://registry.npm.taobao.org/toidentifier/download/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553" integrity sha1-fhvjRw8ed5SLxD2Uo8j013UrpVM= type-is@~1.6.17, type-is@~1.6.18: version "1.6.18" resolved "https://registry.npm.taobao.org/type-is/download/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131" integrity sha1-TlUs0F3wlGfcvE73Od6J8s83wTE= dependencies: media-typer "0.3.0" mime-types "~2.1.24" unpipe@1.0.0, unpipe@~1.0.0: version "1.0.0" resolved "https://registry.npm.taobao.org/unpipe/download/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec" integrity sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw= utils-merge@1.0.1: version "1.0.1" resolved "https://registry.npm.taobao.org/utils-merge/download/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM= vary@~1.1.2: version "1.1.2" resolved "https://registry.npm.taobao.org/vary/download/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=
1.2 启动模拟服务器
1.3 访问模拟服务器
2、测试请求
2.1 代码结构
2.2 main.js
//引入Vue import Vue from 'vue' //引入App import App from './App.vue' //关闭Vue生产提示 Vue.config.productionTip=false; // 创建Vm const vm = new Vue( { el:'#app', render: (h) => h(App), //添加全局事件总线对象 beforeCreate(){ Vue.prototype.$bus=this; } });
2.3 app.vue
<!-- 1:下载并引入 axios PS E:\Vue_demo\VueCli\vue_test> npm i axios added 6 packages in 20s --> <template> <div id="root"> <button @click="getStudents"> 获取学生信息 </button> </div> </template><script> // 2:引入 axios import axios from 'axios'; export default { name: 'App', methods:{ getStudents(){ axios.get('http://localhost:5000/students').then( // 成功返回结果 response =>{ console.log('请求成功',response.data); }, // 错误返回结果 error =>{ console.log('请求失败',error.message); } ); } } } </script>
3、访问请求结果
4、问题分析
根据信息提供:该问题是跨域请求返回得结果后,浏览器判断这位跨域请求,不符安全要求不给提供接收到的数据信息并跑出异常。
处理方案: 1 使用 cors
2 使用 jsonp 的 script src 调用get请求
3 使用 代理服务器方式:如 nginx 、vue脚手架的 devServer.proxy
5、使用Vue的脚手架代理服务器
5.1:配置代理服务器并启动
const { defineConfig } = require('@vue/cli-service') module.exports = defineConfig({ pages:{ index:{ // 入口 entry:'src/main.js', }, }, transpileDependencies: true, lintOnSave:false, /*关闭语法检查*/ //配置vue脚手架的代理服务器:开启代理服务器 此处代理服务器默认与 该脚手架启动的模拟的前端服务器 端口相同:8080 devServer:{ proxy:'http://localhost:5000/' } })
6、调整App.vue的调用请求代码信息并查看界面效果
<!-- 1:下载并引入 axios PS E:\Vue_demo\VueCli\vue_test> npm i axios added 6 packages in 20s --> <template> <div id="root"> <button @click="getStudents"> 获取学生信息 </button> </div> </template>
<script> // 2:引入 axios import axios from 'axios'; export default { name: 'App', methods:{ getStudents(){ axios.get('http://localhost:8080/students').then( // 成功返回结果 response =>{ console.log('请求成功',response.data); }, // 错误返回结果 error =>{ console.log('请求失败',error.message); } ); } } } </script>
说明:
该配置代理方式有一下2点不完美的问题点:
1:不能灵活的配置多个代理服务请求地址
2:如下图的代理服务器的 public文件夹路径下的没有的内容才会服务代理请求的目标服务器上的内容。
7:代理服务器 多个转发地址的设置:
7.1:界面效果
7.2: vue.config.js
const { defineConfig } = require('@vue/cli-service') module.exports = defineConfig({ pages:{ index:{ // 入口 entry:'src/main.js', }, }, transpileDependencies: true, lintOnSave:false, /*关闭语法检查*/ //配置vue脚手架的代理服务器:开启代理服务器 此处代理服务器默认与 该脚手架启动的模拟的前端服务器 端口相同:8080 // 方式一 /* devServer:{ proxy:'http://localhost:5000/' }, */ // 方式二 devServer:{ proxy:{ // '/api': 请求前缀 '/api':{ target:'http://localhost:5000/', // 将请求过来的url信息里的 请求前缀 格式化去除掉 pathRewrite:{'^/api':''}, // ws:true, //用于支持 websocket // chageOrigin:true // 用于控制请求头中的host值 } , // '/api': 请求前缀 '/demo':{ target:'http://localhost:5001/', // 将请求过来的url信息里的 请求前缀 格式化去除掉 pathRewrite:{'^/demo':''}, // ws:true, //用于支持 websocket // chageOrigin:true // 用于控制请求头中的host值 } } }, })
7.3 App.vue
<!-- 1:下载并引入 axios PS E:\Vue_demo\VueCli\vue_test> npm i axios added 6 packages in 20s --> <template> <div id="root"> <button @click="getStudents"> 获取学生信息 </button> <button @click="getCars"> 获取汽车信息 </button> </div> </template><script> // 2:引入 axios import axios from 'axios'; export default { name: 'App', methods:{ getStudents(){ // http://localhost:8080/api/students http://代理服务器ip地址:代理服务器端口/转发请求前缀/原访问系统地址请求信息 axios.get('http://localhost:8080/api/students').then( // 成功返回结果 response =>{ console.log('请求成功',response.data); }, // 错误返回结果 error =>{ console.log('请求失败',error.message); } ); }, getCars(){ debugger; // http://localhost:8080/demo/students http://代理服务器ip地址:代理服务器端口/转发请求前缀/原访问系统地址请求信息 axios.get('http://localhost:8080/demo/cars').then( // 成功返回结果 response =>{ console.log('请求成功',response.data); }, // 错误返回结果 error =>{ console.log('请求失败',error.message); } ); } } } </script>
7.4: main.js
//引入Vue import Vue from 'vue' //引入App import App from './App.vue' //关闭Vue生产提示 Vue.config.productionTip=false; // 创建Vm const vm = new Vue( { el:'#app', render: (h) => h(App), //添加全局事件总线对象 beforeCreate(){ Vue.prototype.$bus=this; } });
为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/