jquery在异步提交方面封装的很好,直接用ajax非常麻烦,jquery大大简化了我们的操作,不用考虑浏览器的诧异了。
$.post、$.get是一些简单的方法,如果要处理复杂的逻辑,还是需要用到jquery.ajax()。
一、$.ajax的一般格式
$.ajax({
type: 'post',
url: url ,
data: data ,
success: success ,
datatype: datatype
});
二、$.ajax的参数描述
参数 描述
三、$.ajax需要注意的一些地方:
1.data主要方式有三种,html拼接的,json数组,form表单经serialize()序列化的;通过datatype指定,不指定智能判断。
2.$.ajax只提交form以文本方式,如果异步提交包含<file>上传是传过不过去,需要使用jquery.form.js的$.ajaxsubmit
四、$.ajax我的实际应用例子
//1.$.ajax带json数据的异步请求
var aj = $.ajax( {
url:'productmanager_reverseupdate',// 跳转到 action
data:{
selrollback : selrollback,
seloperatorscode : seloperatorscode,
provincecode : provincecode,
pass2 : pass2
},
type:'post',
cache:false,
datatype:'json',
success:function(data) {
if(data.msg =="true" ){
// view("修改成功!");
alert("修改成功!");
window.location.reload();
}else{
view(data.msg);
}
},
error : function() {
// view("异常!");
alert("异常!");
}
});
//2.$.ajax序列化表格内容为字符串的异步请求
function notips(){
var formparam = $("#form1").serialize();//序列化表格内容为字符串
$.ajax({
type:'post',
url:'notice_notipsnotice',
data:formparam,
cache:false,
datatype:'json',
success:function(data){
}
});
}
//3.$.ajax拼接url的异步请求
var yz=$.ajax({
type:'post',
url:'validatepwd2_checkpwd2?password2='+password2,
data:{},
cache:false,
datatype:'json',
success:function(data){
if( data.msg =="false" ) //服务器返回false,就将validatepassword2的值改为pwd2error,这是异步,需要考虑返回时间
{
textpassword2.html("<font color='red'>业务密码不正确!</font>");
$("#validatepassword2").val("pwd2error");
checkpassword2 = false;
return;
}
},
error:function(){}
});
//4.$.ajax拼接data的异步请求
$.ajax({
url:'<%=request.getcontextpath()%>/kc/kc_checkmernameunique.action',
type:'post',
data:'mername='+values,
async : false, //默认为true 异步
error:function(){
alert('error');
},
success:function(data){
$("#"+divs).html(data);
}
});
更多jquery ajax。
