文章目录
概述
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。
特性:
- 从浏览器中创建 XMLHttpRequests
- 从 node.js 创建 http 请求
- 支持 Promise API
- 拦截请求和响应
- 转换请求数据和响应数据
- 取消请求
- 自动转换 JSON 数据
- 客户端支持防御 XSRF
这一篇文章我们主要使用Axios对原生Ajax的封装特性
Axios基本使用
第一步:我们先把Axios下载下来
npm install axios
安装完毕后如果你是使用的maven创建的web项目,会多出来一个文件夹
node_modules
,你在里面可以看到axios
当然我们不能直接在web项目中引入一个本地资源,我们可以把里面的核心文件拖入到项目中
第二步:使用axios 发送请求,并获取响应结果
-
发送 get 请求
axios({ method:"get", url:"http://localhost:8080/ajax-demo1/aJAXDemo1?username=zhangsan" }).then(function (resp){ alert(resp.data); })
-
发送 post 请求
axios({ method:"post", url:"http://localhost:8080/ajax-demo1/aJAXDemo1", data:"username=zhangsan" }).then(function (resp){ alert(resp.data); });
axios()
是用来发送异步请求的,小括号中使用 js 对象传递请求相关的参数:
method
属性:用来设置请求方式的。取值为get
或者post
。url
属性:用来书写请求的资源路径。如果是get
请求,需要将请求参数拼接到路径的后面,格式为:url?参数名=参数值&参数名2=参数值2
。data
属性:作为请求体被发送的数据。也就是说如果是post
请求的话,数据需要作为data
属性的值。
then()
需要传递一个匿名函数。我们将 then()
中传递的匿名函数称为 回调函数,意思是该匿名函数在发送请求时不会被调用,而是在成功响应后调用的函数。而该回调函数中的 resp
参数是对响应的数据进行封装的对象,通过 resp.data
可以获取到响应的数据。
我们简单的使用Axios发送一个请求,将请求参数打印的同时对服务器返回的数据进行呈现:
服务器端:
@WebServlet("/Axios1")
public class ServletAxiosDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("你发送了一个post请求");
String username = request.getParameter("username");
System.out.println(username);
response.getWriter().write("hello.Axios~");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("你发送了一个get请求");
String username = request.getParameter("username");
System.out.println(username);
response.getWriter().write("hello.Axios~");
}
}
前端:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../lib/axios.min.js"></script>
</head>
<body>
</body>
<script>
axios({
method:"get",
url:"http://localhost:8080/tomcat-demo1/Axios1?username=zs"
}).then((res) => {
alert(res.data)
})
axios({
method:"post",
url:"http://localhost:8080/tomcat-demo1/Axios1",
data:"username=zs"
}).then((res) => {
alert(res.data)
})
</script>
</html>
Axios请求方式别名
为了方便起见, Axios 已经为所有支持的请求方法提供了别名。如下:
-
get
请求 :axios.get(url[,config])
-
delete
请求 :axios.delete(url[,config])
-
head
请求 :axios.head(url[,config])
-
options
请求 :axios.option(url[,config])
-
post
请求:axios.post(url[,data[,config])
-
put
请求:axios.put(url[,data[,config])
-
patch
请求:axios.patch(url[,data[,config])
注意
在使用别名方法时, url、method、data 这些属性都不必在配置中指定。
例如我们上面代码中的:
axios({
method:"get",
url:"http://localhost:8080/tomcat-demo1/Axios1?username=zs"
}).then((res) => {
alert(res.data)
})
可以变为:
axios.get("http://localhost:8080/tomcat-demo1/Axios1?username=zs").then((res) => {
alert(res.data)
})
axios({
method:"post",
url:"http://localhost:8080/tomcat-demo1/Axios1",
data:"username=zs"
}).then((res) => {
alert(res.data)
})
可以变为:
axios.post("http://localhost:8080/tomcat-demo1/Axios1","username=zs").then((res) => {
alert(res.data)
})
两者的优缺点:
使用别名的方式阅读性较差,但代码简洁
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/122099.html