ajax基本格式:
$.ajax( {
type : "POST",
async : false,
//ajax同步
url : "*.action",
data : {
"userName" : value
},
dataType : "json",
success : function(data) {
var json = eval("(" + data + ")");
if (json["user"] == true) {
} else {
res = true;
}
},
error : function(data) {
alert("error");
}
});
data参数传递需要传递的参数,后台可以用getParamer()来获取这个参数
<pre name="code" class="javascript"> 单个参数形式: data : {
"userName" : loginName
},
多个参数形式: <pre name="code" class="javascript"> data : {
"userName" : loginName,
<pre name="code" class="java"><pre name="code" class="java"><pre name="code" class="javascript"> "userName" : loginName,
},当接收后台传回参数时候,可以这样遍历传回的json类型的list:
第一种(each方法遍历):
$.each(arr,function(idx,item){
//输出
alert(item.id+"哈哈"+item.name);
})
第二种(for方法遍历):for(var key in arr){
alert(key);
alert(arr[key].status);
}
需要强转是可以采用以下两种方法:
$.each(JSON.parse(json), function(idx, obj) {
alert(obj.tagName);
});
//or
$.each($.parseJSON(json), function(idx, obj) {
alert(obj.tagName);
});
注意:每次遍历都可以使用this对象来获取对应值