2011-05-04 82 views
0

其中i有放置在POST方法Ajax中的頁眉和XMHttp請求等在ajax XMLHttp請求中查找標題?

例如: headers.put("X-PAYPAL-SECURITY-USERID", "tok261_biz_api.abc.com"); headers.put("X-PAYPAL-SECURITY-PASSWORD","1244612379");

的Ajax:

$.ajax({ 
    type:'POST', 
    url:'url', 
    data: dataobject, 
    cache:false, 
    dataType:'json', 
    success:onSuccess, 
    error:function(xhr,ajaxOptions){ 
     alert(xhr.status + " :: " + xhr.statusText); 
        } 
     }); 

的XMLHTTP:

var http = new XMLHttpRequest(); 
http.open("POST", url, true); 

http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
http.setRequestHeader("Content-length", params.length); 
http.setRequestHeader("Connection", "close"); 

http.onreadystatechange = function() { 
if(http.readyState == 4 && http.status == 200) { 
    alert(http.responseText); 
} 

回答

2

如果這是jQuery 1.5你可以使用headers屬性:

$.ajax({ 
    type:'POST', 
    url:'url', 
    headers: { 
     "X-PAYPAL-SECURITY-USERID": "tok261_biz_api.abc.com", 
     "X-PAYPAL-SECURITY-PASSWORD": "1244612379" 
    }, 
    data: dataobject, 
    cache:false, 
    dataType:'json', 
    success:onSuccess, 
    error: function(xhr,ajaxOptions) { 
     alert(xhr.status + " :: " + xhr.statusText); 
    } 
}); 

在以前的版本中,你可以使用beforeSend方法:

$.ajax({ 
    type:'POST', 
    url:'url', 
    beforeSend: function(xhr) { 
     xhr.setRequestHeader('X-PAYPAL-SECURITY-USERID', 'tok261_biz_api.abc.com'); 
     xhr.setRequestHeader('X-PAYPAL-SECURITY-PASSWORD', '1244612379'); 
    }, 
    data: dataobject, 
    cache:false, 
    dataType:'json', 
    success:onSuccess, 
    error: function(xhr,ajaxOptions) { 
     alert(xhr.status + " :: " + xhr.statusText); 
    } 
});