欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

axios的跨域处理

程序员文章站 2022-03-16 08:24:54
...

1.下载axios,并在main.js中引入

  1. npm i axios -S
  2. import Axios from 'axios'
  3. Vue.prototype.$axios = Axios //在main.js引入axios,因为其不属于vue全家桶,所以将其挂载在vue原型上,实现全局使用

2.配置config文件中的代理地址

在项目config目录下的修改 index.js文件

  1. proxyTable: {
  2. "/api":{
  3. target: "http://screen.rdxsaj.com",
  4. changeOrigin: true,
  5. pathRewrite: {
  6. '^/api':''
  7. }
  8. }
  9. },

配置完后记得重启项目才会生效

  1. that.$indicator.open({
  2. text:'正在登录...'
  3. })
  4. var obj = {
  5. token:'1AEA4006B1F1F19E26499A75B456EAFD',
  6. deviceSerial:'802883535,D11282193',
  7. appkey:5001,
  8. time:1619071028
  9. }
  10. var url = '/api/v1/vedio';
  11. that.$axios.post(url,obj)
  12. .then(response => {
  13. console.log(response);
  14. setTimeout(function(){
  15. that.$indicator.close();
  16. that.$toast({
  17. message:'登录成功',
  18. // iconClass: 'icon icon-success',
  19. duration:1000
  20. })
  21. },1000);
  22. }).catch(error => {
  23. console.log(error);
  24. })

跨域完成,可以请求到接口了
axios的跨域处理