ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

VUE跨域设置

2021-05-14 22:05:55  阅读:137  来源: 互联网

标签:VUE http 跨域 form api user 设置 password name


根目录config文件夹:
在这里插入图片描述

proxyTable 是 webpack-dev-server 的功能,应该把相关配置放到 Webpack 配置里。

proxyTable: {
      '/api': {
        target: 'http://127.0.0.1:8011/',  //这里后台的地址模拟的;应该填写你们真实的后台接口
        changOrigin: true,  //允许跨域
        pathRewrite: {
          /* 重写路径,当我们在浏览器中看到请求的地址为:http://localhost:8080/api/core/getData/userInfo 时
            实际上访问的地址是:http://121.121.67.254:8185/core/getData/userInfo,因为重写了 /api
           */
          '^/api': ''
        }
      },
    },

dev里面:
在这里插入图片描述
proxyTable只用写api那条就行了

访问地址:api/accounts/login/

POST:

this.$http.post("api/accounts/login/", {name: this.form.name, password: this.form.password }, {emulateJSON: true}).then((response) => {
          console.log(response.body);
          this.user = response.body.user;
          for (var i = 0; i < this.user.length; i++) {
            if (this.user[i].name === this.form.name && this.user[i].password === this.form.password) {
              this.form.type = this.user[i].type;
              this.setUserInfo(this.form);
              this.$router.push({path: 'home'});
              return;
            }
          }
          this.tips = '密码不正确!';
          this.$message({
            message: this.tips,
            type: 'warning'
          });
        }, response => {
          this.$message({
            message: '数据请求失败',
            type: 'error'
          });
        });

修改完后要重启 npm run server 才能生效

在这里插入图片描述
可以看到/app被正则替换为空字符,实际访问的接口是http://127.0.0.1:8000/accounts/login/

Django跨域设置:
注释掉setting.py的MIDDLEWARE的’django.middleware.csrf.CsrfViewMiddleware’

标签:VUE,http,跨域,form,api,user,设置,password,name
来源: https://blog.csdn.net/weixin_43924621/article/details/116806561

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有