#ajax #post #xmlhttprequest
#ajax #Публикация #xmlhttprequest
Вопрос:
где я должен размещать заголовки в запросах ajax и XMHttp в методе POST, например
Например: 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 amp;amp; http.status == 200) {
alert(http.responseText);
}
Ответ №1:
Если это 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);
}
});