vue封装axios请求springboot 及跨域问题

本文详细阐述了如何在Vue项目中使用axios封装常见的HTTP请求,并解决与SpringBoot后端的跨域问题,通过CORS配置和代码示例进行讲解。
摘要由CSDN通过智能技术生成

/**

  • 封装post请求

  • @param url

  • @param data

  • @returns {Promise}

*/

export function post(url,data = {}){

return new Promise((resolve,reject) => {

axios.post(url,data)

.then(response => {

resolve(response.data);

},err => {

reject(err)

})

})

}

/**

  • 封装patch请求

  • @param url

  • @param data

  • @returns {Promise}

*/

export function patch(url,data = {}){

return new Promise((resolve,reject) => {

axios.patch(url,data)

.then(response => {

resolve(response.data);

},err => {

reject(err)

})

})

}

/**

  • 封装put请求

  • @param url

  • @param data

  • @returns {Promise}

*/

export function put(url,data = {}){

return new Promise((resolve,reject) => {

axios.put(url,data)

.then(response => {

resolve(response.data);

},err => {

reject(err)

})

})

}

main.js  加入

import {post,fetch,patch,put} from “…/static/http.js”;

Vue.prototype.$axios=axios;

Vue.prototype.$post=post;

Vue.prototype.$fetch=fetch;

Vue.prototype.$patch=patch;

Vue.prototype.$put=put;

import Vue from ‘vue’

import App from ‘./App’

import router from ‘./router’

import ElementUI from ‘element-ui’;

import ‘element-ui/lib/theme-chalk/index.css’;

// table 的样式需要手动引入

import ‘element-ui/lib/theme-chalk/icon.css’

import ‘element-ui/lib/theme-chalk/table.css’

import ‘element-ui/lib/theme-chalk/table-column.css’

import store from ‘./store’

import Vuex from ‘vuex’

import axios from ‘axios’

import {post,fetch,patch,put} from “…/static/http.js”;

Vue.prototype.$axios=axios;

Vue.prototype.$post=post;

Vue.prototype.$fetch=fetch;

Vue.prototype.$patch=patch;

Vue.prototype.$put=put;

Vue.config.productionTip = false

Vue.use(ElementUI);

axios.defaults.headers.post[‘Content-Type’] = ‘application/json;charset=UTF-8’;

Vue.use(Vuex);

/* eslint-disable no-new */

new Vue({

el: ‘#app’,

router,

store,

render: h => h(App)

})

vue页面请求

methods:{

SubmitLogin:function () {

var params = {

userid:1001

};

this.$post(“tuser/findId”,params)

.then((response)=>{

alert(JSON.stringify(response))

})

}

},

springboot后端:

Controller

//前端传过来的是json类型的值 需要加上这个注解@RequestBody 不然接收不到值

@RequestMapping(“findId”)

@ResponseBody

public TUser findId(@RequestBody TUser tUser){

System.out.println(tUser.getUserid());

return tuserService.getTuseId(tUser.getUserid());

}

service

@Override

public TUser getTuseId(Integer id) {

return tuserMapper.getTuseId(id);

}

mapper

/**

  • 根据id查询

  • @param id

  • @return

*/

public TUser getTuseId(Integer id);

mapper xml

<?xml version="1.0" encoding="UTF-8" ?>

SELECT * FROM t_user where userid=#{id};

执行后浏览器控制台出现了

跨域问题

前端config下的index.js加上

解决方法:

后端建个CorsConfig

package com.spring.mybatis.config;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.CorsRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**

  • WebMvcConfigurerAdapter

  • @author heng

  • @version 1.0.0

**/

/**

  • 解决vue+spring boot跨域问题

*/

@Configuration

public class CorsConfig extends WebMvcConfigurerAdapter{

@Override

public void addCorsMappings(CorsRegistry registry) {

System.out.println(“----------------------”);

registry.addMapping(“/**”)

.allowedOrigins(“*”)

.allowCredentials(true)

.allowedMethods(“GET”, “POST”, “DELETE”, “PUT”)

.maxAge(3600);

}

}

最后运行:

还有一种方法 加上过滤器 过滤

启动类代码加上 @ServletComponentScan

package com.spring.mybatis;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.web.servlet.ServletComponentScan;

import org.springframework.context.annotation.ComponentScan;

/**

  • Application

  • @author heng

  • @version 1.0.0

**/

@SpringBootApplication

@MapperScan(“com.spring.mybatis.mapper”)

@ComponentScan(“com.spring.mybatis”)

@ServletComponentScan

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class,args);

}

}

创建过滤器

ACAFilter

package com.spring.mybatis.filter;

import org.springframework.core.annotation.Order;

import javax.servlet.*;

import javax.servlet.annotation.WebFilter;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

/**

  • 跨域的设置问题

  • @author tonywu

  • @version v1.0.0

*/

//@Component //在启动类加了@ServletComponentScan无需加这个了

@Order(3)//设置优先级加载

//@ServletComponentScan //加载启动类上了

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(资料价值较高,非无偿)

最后

前端校招精编面试解析大全点击这里即可获取完整版pdf查看

己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-QVXYc6T4-1711648085025)]

[外链图片转存中…(img-16jqY66Z-1711648085026)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

[外链图片转存中…(img-AkETjRJ7-1711648085027)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(资料价值较高,非无偿)

最后

前端校招精编面试解析大全点击这里即可获取完整版pdf查看

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值