1. 程式人生 > >spring mvc ajax 和axios 的 跨域問題

spring mvc ajax 和axios 的 跨域問題

mvc 效果 setname ios 技術分享 pat 記錄 creat 不錯

今天在使用springmvc 做vue的接口時 遇到了跨域問題,記錄一下,

axios 代碼

this.axios.get(‘http://localhost:8989/firestmavenspringmvc/createonefee‘, {
OncFeeName: ‘2323z鐘偉a‘,
price: ‘2323‘
},config).then((res) => {
console.log(res);
this.$router.push({ path: this.redirect || ‘/‘ })
this.loading = false
}).catch((error) => {
console.log(error);
this.loading = false
})
springmvc 代碼

@RequestMapping(value = "/createonefee", method = RequestMethod.GET)
public @ResponseBody resultmodel createonefee() {
resultmodel data = new resultmodel();
data.setId("1");
data.setName("小夥子不錯喲 get");
data.setDatetime(new Date());
return data;}

springmvc 配置文件

<mvc:cors>
<mvc:mapping path="/**" /> //可以配置到具體的controller和方法
</mvc:cors>

我的環境是spring 4.2 所以只需要在配置文件中配置就好了,如果是spring 3一下就需要寫一個適配器

下面是效果

技術分享圖片

spring mvc ajax 和axios 的 跨域問題