≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

分析VueJs前后端分离跨域问题

人气:452 时间:2019-04-07

这篇文章主要为大家详细介绍了分析VueJs前后端分离跨域问题,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

 

使用vue-cli搭建的vue项目

 

可以使用在项目内设置代理(proxyTable)的方式来解决跨域问题

设置配置项的目录在config下的index.js,主要通过配置proxyTable项,设置代理指向你的后台地址

代码如下:


dev: {
  env: require('./dev.env'),
  port: 8085,
  autoOpenBrowser: true,
  assetsSubDirectory: 'static',
  assetsPublicPath: '/',
  proxyTable: {
   '/agent': {
    target: 'http://127.0.0.1:7105/',
    changeOrigin: true,
    pathRewrite: {
     '^/agent': ''
    }
   }
  },
  // CSS Sourcemaps off by default because relative paths are "buggy"
  // with this option, according to the CSS-Loader README
  // (https://github.com/webpack/css-loader#sourcemaps)
  // In our experience, they generally work as expected,
  // just be aware of this issue when enabling this option.
  cssSourceMap: false
 }

前端使用vue-resource来发起请求时

代码如下:


//在main.js中设置公用的地址
Vue.prototype.rootUrl = '/agent/';

//在具体page中发起请求的方式
that.$http.post(this.rootUrl + 'login', parms).then(function (response) {
     // 响应成功回调
     console.log(response);
    }, function (response) {
     // 响应错误回调
    });

其他方式搭建的前端项目,通过使用nginx启动前端服务同时配置代理

下列是我的nginx配置文件,不管是通过什么方式搭建的前端项目,构建成功后都会输出一个dist文件,我们只需要将nginx服务目录指向你的dist文件下你项目的入口文件即可

我的文件目录是 root D:\openplatform\portal\webapp\dist; 更改此条配置到你的目录 我入口文件名称是index.html 使用的是vue-cli打包的项目,参考vue-cli npm run build的dist目录,指向那个目录下

代码如下:


#user nobody;
worker_processes 4;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid    logs/nginx.pid;

events {
  worker_connections 1024;
}

http {
  include    mime.types;
  default_type application/octet-stream;

  #log_format main '$remote_addr - $remote_user [$time_local] "$request" "$status" $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" "$gzip_ratio" $request_time $bytes_sent $request_length';

  log_format main '[$time_iso8601] [$remote_addr] [$request] [$http_user_agent] [$cookie_customerID_cookie_flag] [$args]';

  access_log logs/access.log main;

  sendfile    on;
  #tcp_nopush   on;

  #keepalive_timeout 0;
  keepalive_timeout 65;

  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 3;
  gzip_proxied any;
  gzip_types *;

  server {
    listen     80;
    root D:\openplatform\portal\webapp\dist;
    index index.html;

    location / {
       try_files $uri $uri/ @router;
       index index.html;
      }
  location @router {
       rewrite ^.*$ /index.html last;
    }
  location ^~/agent/ {
      proxy_pass  http://127.0.0.1:7105/;
      proxy_redirect  http://127.0.0.1:7105/ /;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header REMOTE-HOST $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_connect_timeout 600s;
      proxy_read_timeout 600s;
      proxy_send_timeout 600s;
    }
  }


  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #  listen    8000;
  #  listen    somename:8080;
  #  server_name somename alias another.alias;

  #  location / {
  #    root  html;
  #    index index.html index.htm;
  #  }
  #}


  # HTTPS server
  #
  #server {
  #  listen    443 ssl;
  #  server_name localhost;

  #  ssl_certificate   cert.pem;
  #  ssl_certificate_key cert.key;

  #  ssl_session_cache  shared:SSL:1m;
  #  ssl_session_timeout 5m;

  #  ssl_ciphers HIGH:!aNULL:!MD5;
  #  ssl_prefer_server_ciphers on;

  #  location / {
  #    root  html;
  #    index index.html index.htm;
  #  }
  #}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持四海网。

本文来自:http://www.q1010.com/184/3494-0.html

注:关于分析VueJs前后端分离跨域问题的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • 基于VUE选择上传图片并页面显示(图片可删除)
  • vue.js动态数据绑定学习笔记
  • 分析vue嵌套路由-params传递参数
  • 分析Vue中过度动画效果应用
  • vue.js 左侧二级菜单显示与隐藏切换的实例代码
  • 分析Vue使用命令行搭建单页面应用
  • 基于vue实现swipe分页组件实例
  • vue.js实现价格格式化的方法
  • 基于Vue的文字跑马灯组件(npm 组件包)
  • Vue.js对象转换实例
上一篇:vue-cli+webpack在生成的项目中使用bootstrap实例代码
下一篇:基于VUE选择上传图片并页面显示(图片可删除)
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。