在jquery的ajax中添加自定義的header信息

 轉自網絡
1
$.ajax({ 2 type: "POST", 3 url: "http://192.168.0.88/action.cgi?ActionID=WEB_RequestCertificateAPI", 4 data: { 5 "user": "api", 6 "password": "api" 7 }, 8 dataType: "json", 9 ContentType: "application/json", 10 beforeSend: function (XMLHttpRequest) { 11 //HttpUtility.UrlEncode 12 var sessionid = "SessionID"; 13 XMLHttpRequest.setRequestHeader(sessionid, sid); 14 }, 15 success: function (SessionID) { 16 //alert(SessionID.success); 17 var dt = eval("(" + SessionID.data + ")"); 18 if (SessionID.success == 1) { 19 alert("success"); 20 } 21 else { 22 alert('errorId:' + SessionID.error.id); 23 alert('errorCode:' + SessionID.error.code); 24 alert('errorPara:' + SessionID.error.params); 25 } 26 } 27 });由於種種緣由,本身的框架中要傳系統約定好的header信息,頁面使用的jquery的ajax請求,找了很久,忽然發現咱們能夠在beforeSend方法中設置,這個方法接受一個參數,就表明了發起異步請求的XMLHttpRequest對象,咱們能夠用該對象的setRequestHeader方法實現咱們的目的,好比,我在header中加一個自定義的dataType頭信息:beforeSend: function(XMLHttpRequest) {         XMLHttpRequest.setRequestHeader("dataType","abc");          $(".return_confrim").html("<img src='jquery/images/loading.gif'/>&nbsp;&nbsp;&nbsp;&nbsp;正在驗證帳戶...");},這樣,咱們在服務器端能夠獲得自定義中的dataType信息。
相關文章
相關標籤/搜索