vue封装axios请求springboot 及跨域问题,web保活面试题

})

}

/**

  • 封装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;

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

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

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

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

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

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注前端)
img

最后

推荐一些系统学习的途径和方法。

路线图

每个Web开发人员必备,很权威很齐全的Web开发文档。作为学习辞典使用,可以查询到每个概念、方法、属性的详细解释,注意使用英文关键字搜索。里面的一些 HTML,CSS,HTTP 技术教程也相当不错。

CodeChina开源项目:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

HTML 和 CSS:

html5知识

css基础知识

片转存中…(img-QOABTvWX-1712007884091)]

最后

推荐一些系统学习的途径和方法。

路线图

每个Web开发人员必备,很权威很齐全的Web开发文档。作为学习辞典使用,可以查询到每个概念、方法、属性的详细解释,注意使用英文关键字搜索。里面的一些 HTML,CSS,HTTP 技术教程也相当不错。

CodeChina开源项目:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

HTML 和 CSS:

html5知识

css基础知识

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值