Axios快速入门
Axios快速入门
-
引入axios的js文件
<script src="js/axios-0.18.0.js"></script>
-
使用axios发送请求,并获取响应结果
<!--引入axios的js文件-->
<script src="js/axios-0.18.0.js"></script>
<script>
// 1. get
/*axios({
method:"get",
url:"http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan"
}).then(function(resp){
alert(resp.data)
});*/
// 2. post
axios({
method:"post",
url:"http://localhost:8080/ajax-demo/axiosServlet",
data: "username=zhangsan"
}).then(function(resp){
alert(resp.data)
});
</script>
Axios请求方式别名
-
为了方便起见,Axios已经为所有支持的请求方法提供了别名
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.options(url[, config])
axios.post(url[, config])
axios.put(url[, config])
axios.patch(url[, config])
-
发送get请求
axios.get("url) .then(function (resp){ alter(resp.data) });
-
发送post请求
axios.post("url", "参数") .then(function (resp){ alert(resp.data); });
<body>
<script src="js/axios-0.18.0.js"></script>
<script>
// 1. get
/*axios({
method:"get",
url:"http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan"
}).then(function(resp){
alert(resp.data)
});*/
axios.get("http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan")
.then(function (resp){
alert(resp.data)
});
// 2. post
/*axios({
method:"post",
url:"http://localhost:8080/ajax-demo/axiosServlet",
data: "username=zhangsan"
}).then(function(resp){
alert(resp.data)
});*/
axios.post("http://localhost:8080/ajax-demo/axiosServlet", "username=zhangsan")
.then(function (resp){
alert(resp.data)
});
</script>
</body>