springCloud+vue3.0 在vue中配置跨域,通过axios调用Java接口

本文介绍了在Vue3项目中配置跨域代理以调用SpringCloud后端Java接口的方法。首先在vue.config.js中设置devServer的proxy属性,然后在main.js中引入axios进行接口调用。示例包括GET请求的两种参数传递方式及POST请求。同时展示了Java接口的定义,包括不同参数接收方式。
摘要由CSDN通过智能技术生成

**

springCloud+vue3.0 在vue中配置跨越,通过axios调用Java接口

**
今年4月份换的工作,由于不太熟悉cloud,导致找的工作不是特别满意,就想着自己搞个项目练练(嘿嘿,来年方便跑路),虽然是练手,还是想搞个有界面的,因此用了vue3,但是在请求后端数据时搞了半天,网上查到的都是vue2的方法,因此在这里记录一下,希望能帮到大家

vue中配置跨越

在vue.config.js中配置如下代码(vue3是没有这个文件的,在根目录下创建一个)

module.exports = {
    devServer: {
        proxy: 'http://localhost:8001'
    }
}

http://localhost:8001 此路径是调用Java接口路径的前半部分
在这里插入图片描述

main.js中引入axios

在这里插入图片描述

axios调用Java接口

1. 在当前js中先引入axios

import axios from 'axios';

请求Java接口,传参方式一

axios.get('/find/1')   //http://localhost:8080/find/1
.then(obj=>{
    console.log("请求成功");
    console.log(obj.data);
}).catch(err=>{
    console.log("请求失败");
    console.log(err);
})

请求Java接口,传参方式二

axios({         //http://localhost:8080/find?id=1&name=ycz
    url: '/find',
    method: 'get',
    params: {
        'id': 2,
        'name':"ycz"
    },
})
.then(function (response) {
    console.log(response.data);
})
.catch(function (error) {
    console.log(error);
});

提交数据,post请求

var data = {
    code: "666666",
    create_time: null,
    creator: "xxx",
    delflag: 0,
    edit_time: "2021-08-07 00:00:00",
    editor: "xxx",
    maindepid: null,
    mainposid: null,
    name: "admin",
    sex: null,
}
axios({
    url: '/add',
    method: 'post',
    data: data,
})
.then(function (response) {
    console.log(response);
})
.catch(function (error) {
    console.log(error);
});

Java接口

	//http://localhost:8001/find/1      请求路径法式一
	//@RequestMapping(value = "/find/{id}", method = RequestMethod.GET)
	//public HrUser findUserById(@PathVariable("id") Long id) {
	
	//http://localhost:8001/find?id=1      请求路径法式二
	@RequestMapping(value = "/find", method = RequestMethod.GET)
	public HrUser findUserById(Long id,String name) {
		System.out.println(id+"----------"+name);
		return user.findUserById(id);
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public List<HrUser> findAllUser () {
		return user.findAllUser();
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public boolean addUser (@RequestBody HrUser users) {
		System.out.println(99);
		return user.addUser(users);
	}

说明:findUserById方法被调用时传参的方式
1. 请求接口传参法式一

	//http://localhost:8001/find/1      
	@RequestMapping(value = "/find/{id}", method = RequestMethod.GET)
	public HrUser findUserById(@PathVariable("id") Long id) {
	System.out.println(id+"----------"+name);
		return user.findUserById(id);
	}

2. 请求接口传参法式二

	//http://localhost:8001/find?id=1      
	@RequestMapping(value = "/find", method = RequestMethod.GET)
	public HrUser findUserById(Long id,String name) {
		System.out.println(id+"----------"+name);
		return user.findUserById(id);
	}
Vue使用axios调用多个接口跨域问题可以通过设置代理来解决。 1. 首先,在Vue项目根目录下找到config文件夹的index.js文件。 2. 打开index.js文件,找到dev对象的proxyTable选项。该选项允许我们配置代理。 3. 在proxyTable选项添加以下代码: ```javascript proxyTable: { '/api1': { target: 'http://localhost:8000', changeOrigin: true, pathRewrite: { '^/api1': '/api1' } }, '/api2': { target: 'http://localhost:8001', changeOrigin: true, pathRewrite: { '^/api2': '/api2' } } } ``` 此处使用了两个接口作为示例,一个是/api1,另一个是/api2。target参数指定了代理的目标地址,需要根据实际情况进行修改。changeOrigin参数用于配置是否跨域,默认为false,需要设置为true。pathRewrite参数用于重写请求路径,可以根据需要进行修改。 4. 保存并关闭index.js文件。 5. 在Vue组件使用axios调用接口时,只需要将接口的路径改为对应的代理路径,例如: ```javascript axios.get('/api1/users') .then(response => { // 处理接口1的响应 }) .catch(error => { // 处理接口1的错误 }); axios.get('/api2/posts') .then(response => { // 处理接口2的响应 }) .catch(error => { // 处理接口2的错误 }); ``` 以上代码,/api1和/api2就是代理路径,可以直接在axios使用。 通过以上步骤,我们成功地解决了在Vue使用axios调用多个接口跨域问题。注意,在开发环境下使用代理是安全的,但在生产环境要将代理改为实际的服务器地址。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值