axios說(shuō)明文檔:
https://www.kancloud.cn/yunye/axios/234845
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>復(fù)習(xí)axios </title>
<!-- vue核心 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 引入axios -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
<button type="button" @click="sendget">AjaxGet方式</button>
<button type="button" @click="sendget2">AjaxGet2。submit方式</button>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
},
/* 函數(shù) */
methods: {
/* 出發(fā)ajax觸發(fā)的函數(shù) */
sendget() {
//springboot controller 層的路徑
axios.get('http://localhost:8878/springboot/test?id=20&name=張老飛')
/* 成功后執(zhí)行 */
.then(function(response) {
console.log(response);
console.log(response.data);
})
.catch(function(error) {
//失敗了,
console.log(error);
});
},
sendget2() {
//springboot controller 層的路徑
axios.get('http://localhost:8878/springboot/submit?no=20')
/* 成功后執(zhí)行 */
.then(function(response) {
console.log(response);
console.log(response.data);
})
.catch(function(error) {
//失敗了,
console.log(error);
});
}
},
})
</script>
</body>
</html>
再sprintboot項(xiàng)目中配置跨域的請(qǐng)求
@CrossOrigin(origins="*",maxAge=3600)
后臺(tái)
package com.neuedu.springboot.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.neuedu.springboot.model.bean.Hero_User;
import com.neuedu.springboot.model.service.Hero_UserService;
@Controller
@CrossOrigin(origins="*",maxAge=3600)
//* 所有請(qǐng)求都跨域
//maxAge最大時(shí)間
public class Hero_UserController {
//注入service的依賴
@Autowired
private Hero_UserService serivce;
//@RequestMapping(value="submit" ,method=RequestMethod.POST)
@RequestMapping("submit")
@ResponseBody//返回jsion字符串
public String selectAll(HttpSession session,int no) {
System.out.println("submit");
Hero_User slectno =new Hero_User();
slectno=serivce.getHero_UserNO(no);
System.out.println(slectno);
// List<Hero_User> list=new ArrayList<>();
// list=serivce.getHreo_User();
// for (Hero_User hero_User : list) {
// System.out.println(hero_User);
// }
// session.setAttribute("list", list);
return "{\"result\":\"yes\"}";
}
}