$.ajax({
url:'findUser2.do',
async: 'true',
type:'GET',
dataType:'json',// 说明返回的数据是json格式
success:function(data){
$.each(data,function(idx,item){
if(idx==0){
return true;//同countinue,返回false同break
}
alert(item.user_nam);
});
}
});
以上的data格式为
[{"user_nam":"张三","user_pwd":null,"dattim":"20160101","user_id":"1001"},{"user_nam":"李四","user_pwd":null
,"dattim":"20160101","user_id":"1002"}]
$.ajax({
url:'/user/addUser',
type:'post',
data:{//请求的参数是json格式
'USER_ID':USER_ID,
'USER_NAM':USER_NAM,
'USER_PWD':USER_PWD,
'USER_AREA':USER_AREA,
'DESC_INF':DESC_INF,
'USER_TEL':USER_TEL,
'USER_EMAIL':USER_EMAIL
},
dataType:'json',//说明返回的数据是json
success:function(data){
alert(data['respCode']);
},
error:function(data){
}
});
以上success : function(data) data 的格式 {"respCode":"00000","respMsg":"成功"}
//3.$.ajax拼接url的异步请求
var yz=$.ajax({
type:'post',
url:'userAction?username='+username,
data:{
},
cache:false,
dataType:'json',
success:function(data){
},
error:function(){
}
});
一、$.ajax的一般格式
$.ajax({
type: 'POST', //提交的方式”post“ ”get“
url: url ,
data: data , //一般是键值对 或 json
success: success ,
dataType: dataType //返回数据的格式
});
二、$.ajax的参数描述
| 参数 | 描述 |
url |
必需。规定把请求发送到哪个 URL。 |
data |
可选。映射或字符串值。规定连同请求发送到服务器的数据。 |
success(data, textStatus, jqXHR) |
可选。请求成功时执行的回调函数。 |
dataType |
可选。规定预期的服务器响应的数据类型。 默认执行智能判断(xml、json、script 或 html)。 |
ajax 使用 serialize() ,对于select ,checkbox 等 都 起作用 非常方便。 serialize() 方法通过序列化表单值,创建 URL 编码文本字符串。
1 $.ajax({ 2 type:"POST", 3 url: "/adsadmin/offer/addOffer", 4 dataType:"json", 5 data: $("#myForm").serialize(), // 6 success: function(data) { 7 window.location = "frame.html?goto=show-offer"; 8 }, 9 error: function(XMLHttpRequest, 10 textStatus, errorThrown) { 11 alert(errorThrown); 12 } 13 });
使用$.post()、$.get()和$.getJSON()也是一样的
$.post('url', $("form").serialize(), function(data) {
}
});
$.get('url', $("form").serialize(), function(data) {
}
});
$.getJSON(' url', $("form").serialize(), function(data) {
}
});
getJSON , 参数url后面拼接
1 $.getJSON(url, function(data){ 2 3 });
主意:
1.data主要方式有三种,html拼接的,json数组,form表单经serialize()序列化的;通过dataType指定,不指定智能判断。
2.$.ajax只提交form以文本方式,如果异步提交包含<file>上传是传过不过去,需要使用jquery.form.js的$.ajaxSubmit
作者原创转载请说明:http://www.cnblogs.com/c9999/p/5445285.html