jQuery Ajax 异步请求data参数数据格式的六种方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form id="login" method="post" action="result.jsp">
<input name="user" type="text"/>
<input name="sex" type="radio" value="man"/>
<input name="sex" type="radio" value="woman"/>
interest:
<input type="checkbox" name="interest" value="piu">PIU
<input type="checkbox" name="interest" value="dss">DSS
<input type="checkbox" name="interest" value="ddr">DDR<br>
<input type="button" name="submit" value="submit" onclick="getFormInfo();">
</form>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
//第一种写法(拼接URL)
function getFormInfo(){
var name='wen';
var user='chen';
$.ajax({
url: "/login/authenticate?name="+name+"&user="+user,
type: "POST",
data:{},
dataType: "json",
success: function(data){
},
error:function(err){
console.log(err.statusText);
console.log('异常');
}
});
}
//第二种写法(表单序列化为json数据)
function getFormInfo(){
var params=$('#login').serializeJSON();
console.log(params);
$.ajax({
url: "http://192.168.1.10:8088/login/authenticate",
type: "POST",
data:params,
cache:false,
dataType: "json",
success: function(data){
},
error:function(err){
}
});
}
// 第三种写法(表单序列化为字符串)
function getFormInfo(){
var params=$('#login').serialize();
console.log(params);
$.ajax({
url: "http://192.168.1.10:8088/login/authenticate",
type: "POST",
data:params,
cache:false,
dataType: "json",
success: function(data){
},
error:function(err){
}
});
}
//第四种写法(带json数据)
function getFormInfo(){
$.ajax({
url: "http://192.168.1.10:8088/login/authenticate",
type: "POST",
data:{
name:'chem',
user:'wen'
},
cache:false,
dataType: "json",
success: function(data){
},
error:function(err){
}
});
}
//第五种写法(拼接data)
function getFormInfo(){
var name='chen';
var user='wen';
$.ajax({
url: "http://192.168.1.10:8088/login/authenticate",
type: "POST",
data:'name='+name+'&user='+user,
cache:false,
dataType: "json",
success: function(data){
},
error:function(err){
}
});
}
//第六种写法(既有全部直接获取表单中的数据又有单独出来的数据)
function getFormInfo(){
var params=$('#login').serializeJSON();
console.log(params);
params.height='20';
$.ajax({
url: "http://192.168.1.10:8088/login/authenticate",
type: "POST",
data:params,
cache:false,
dataType: "json",
success: function(data){
},
error:function(err){
}
});
}
</script>
</body>
</html>