解决vue跨域问题

环境搭建

1.服务端

服务端端口8081,服务端有资源"http://localhost:8081/hello"

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("- - - - 8081 /hello - - - -");
        response.getWriter().write("success");
    }

}

2.客户端

客户端端口8080,地址“http://localhost:8080”

App.vue

<template>
  <div>
    <h4>This is 8080</h4>
    <button @click="test">Click Me</button>
  </div>
</template>

<script>

  import $ from 'jquery'

  export default{
      methods:{
        test(){
           $.ajax({
              url:'http://localhost:8081/hello',
              type:'POST',
              success:function (res) {
                  alert(res);
              },
              error:function () {
                  alert("发生错误");
              }
          });
        }
      }
  }
</script>

<style>
</style>

3.运行

发送请求,ajax跨域请求失败

一、修改后端

服务端设置响应头response.setHeader("Access-Control-Allow-Origin", "*");

*代表可访问的地址,可以设置指定域名

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setHeader("Access-Control-Allow-Origin", "*");

        System.out.println("- - - - 8081 /hello - - - -");
        response.getWriter().write("success");
    }

}

结果:

二、修改前端

使用proxyTable设置接口代理(项目使用vue-cli脚手架搭建)

在config/index.js下设置

  proxyTable: {
      '/api':{ //匹配所有以'api'开头的请求路径
        target:'http://localhost:8081', //代理目标的基础路径
        changeOrigin:true,  //是否支持跨域
        pathRewrite:{ //重写路径:去掉路径中开头的'/api'
          '^/api':''
        }
      }
    }

App.vue

<template>
  <div>
    <h4>This is 8080</h4>
    <button @click="test">Click Me</button>
  </div>
</template>

<script>

  import $ from 'jquery'

  export default{
      methods:{
        test(){
           $.ajax({
              url:'/api/hello',
              type:'POST',
              success:function (res) {
                  alert(res);
              },
              error:function () {
                  alert("发生错误");
              }
          });
        }
      }
  }
</script>

<style>
</style>

结果:

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值