一、axios的基本使用
1、axios 发送get请求 带参
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
方式一:
axios({
url: 'http://127.0.0.1/user/signout?id=1',
method: 'get',
}).then(res=>{
console.log(res)
})
方式二:
axios({
url: 'http://127.0.0.1/user/signout',
method: 'get',
params:{
'id': 1 //url上拼接了?id=1
} }).then(res=>{
console.log(res)
})
2、post方式发送请求[无参]
axios({
url: 'http://127.0.0.1/user/signout',
method: 'post',
}).then(res=>{
console.log(res)
})
原生ajax实现
var username = document.getElementById("txtUserName").value;
var password = document.getElementById("txtpassword").value;
{#console.log(username,password);#}
{#console.log(params)#}
$.ajax({
type: "POST",
dataType: "json",//服务器返回的数据类型
contentType: "application/json",//post请求的信息格式
url: "/user/signin",
{#data: $('#form1').serialize(),#}
data: JSON.stringify({
"username":username,
'password':password
}),
success: function (result) {
console.log(result);//在浏览器中打印服务端返回的数据(调试用)
if (result.resultCode == 200) {
alert("SUCCESS");
}
;
},
error: function () {
alert("异常!");
}
});