jquery ajax中data屬性詳解

$.post、$.get是一些簡單的方法,若是要處理複雜的邏輯,仍是須要用到jQuery.ajax() 1、$.ajax的通常格式 $.ajax({ type: 'POST', url: url , data: data ,
   dataType:dataType , success: success ,
error: error }); 2、$.ajax的參數描述 參數 描述 url 必需。規定把請求發送到哪一個 URL。 data 可選。映射或字符串值。規定連同請求發送到服務器的數據。 success(data, textStatus, jqXHR) 可選。請求成功時執行的回調函數。 dataType 可選。規定預期的服務器響應的數據類型。 默認執行智能判斷(xml、json、script 或 html)。 3、$.ajax須要注意的一些地方:
1.data主要方式有三種,html拼接的,json數組,form表單經serialize()序列化的;經過dataType指定,不指定智能判斷。 2.$.ajax只提交form以文本方式,若是異步提交包含<file>上傳是傳過不過去,須要使用jquery.form.js的$.ajaxSubmit 4、$.ajax個人實際應用例子 Js代碼 收藏代碼 //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); } });

原文地址:http://jun1986.iteye.com/blog/1399242html

相關文章
相關標籤/搜索