当前位置: 首页 > news >正文

前端基础之ajax

vue-cli配置代理服务器解决跨域问题

我们可以使用一个代理服务器8080,Vue项目8080发送请求向代理服务器8080发送请求,再由在理服务器转发给后端服务器

首先需要在vue.config.js中配置代理服务器

const { defineConfig } = require('@vue/cli-service')

module.exports = defineConfig({

  transpileDependencies: true,

  lintOnSave: false,

  //开启代理服务器

  devServer:{

    proxy:'http://localhost:5000'

  }

})

然后将发送请求的axios路径改为代理服务器也就是改为8080

<template>

  <div>

    <button @click="getStudents">获取学生消息</button>

  </div>

</template>

<script>

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>

成功接收数据

多个代理配置

如果我们需要接收多个服务器传输的数据,就需要配置不止一个代理

在vue.config.js中

const { defineConfig } = require('@vue/cli-service')

module.exports = defineConfig({

  transpileDependencies: true,

  lintOnSave: false,

  devServer:{

    proxy:{

      '/atguigu':{

        target:'http://localhost:5000',

        pathRewrite:{'^/atguigu':''},

         ws:true,  //用于支持websocket

         changeOrigin:true //用于控制请求头中的host

      },

      '/demo':{

        target:'http://localhost:5001',

        pathRewrite:{'^/demo':''},

         ws:true,  //用于支持websocket

         changeOrigin:true //用于控制请求头中的host

      }  

    }

   

  }

})

进行发送请求

<template>

  <div>

    <button @click="getStudents">获取学生消息</button>

    <button @click="getCars">获取汽车消息</button>

  </div>

</template>

<script>

import axios from 'axios';

export default {

  name: 'App',

  methods:{

    getStudents(){

      axios.get('http://localhost:8080/atguigu/students').then(

        response=>{

          console.log('请求发送成功了',response.data)

        },

        error=>{

          console.log('请求失败了',error.message)

        }

      )

    },

    getCars(){

      axios.get('http://localhost:8080/demo/cars').then(

        response=>{

          console.log('请求发送成功了',response.data)

        },

        error=>{

          console.log('请求失败了',error.message)

        }

      )

    }

  }

}

</script>

相关文章:

  • 【Pandas】pandas Series argmax
  • 工厂模式:简单工厂、工厂方法以及抽象工厂
  • 重定位(2)
  • Qt的QDateTimeEdit控件的使用
  • 并发编程(线程安全)面试题及原理
  • 华为云IAM 用户名和IAM ID
  • 在Blender中给SP分ID通道图
  • 后路式编程
  • Java进阶-SpringCloud设计模式-工厂模式的设计与详解
  • Linux软件包管理
  • 2025最新Transformer模型及深度学习前沿技术应用
  • go语言因为前端跨域导致无法访问到后端解决方案
  • React + TypeScript 实战指南:用类型守护你的组件
  • MVTec HDevelopEVO 24.11体验
  • MsBox.Avalonia 全局修改弹框样式
  • Gradle 配置 Lombok 项目并发布到私有 Maven 仓库的完整指南
  • ​DeepSeek:如何通过自然语言生成HTML文件与原型图?
  • Calico-基本安装、IPIP同节点通信和Proxy_ARP实践 Day01
  • CentOS 7 aarch64上制作kernel rpm二进制包 —— 筑梦之路
  • leetcode day25 28 KMP算法
  • 沪喀同心|为新疆青少年提供科普大餐,“小小博物家(喀什版)”启动
  • 共情场域与可持续发展——关于博物馆、美术馆运营的新思考
  • 云南大理铁路枢纽工程建设取得两大进展,预计明年建成
  • 中方发布会:中美经贸高层会谈氛围是坦诚的、深入的、具有建设性的
  • 上海下周最高气温在30℃附近徘徊,夏天越来越近
  • 印称一名高级官员在巴基斯坦发动的袭击中死亡