引入 axios 的 js 文件
<script src="js/axios-0.18.0.js"></script>
发送 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){
aler
axios() 是用来发送异步请求的,小括号中使用 js 对象传递请求相关的参数:
method 属性:用来设置请求方式的。取值为get或者post
url属性:用来书写请求的资源路径。如果是get请求,需要将请求参数拼接到路径的后面,格式为: url?参数名=参数值&参数名2=参数值
data属性:作为请求体被发送的数据。也就是说如果是post请求的话,数据需要作为data属性值
then()需要传递一个匿名函数,成为回调函数,也就是成功响应后调用的函数,resp参数是对响应的数据进行封装的对象,通过resp.data可以获取到响应的数据