admin管理员组文章数量:1277347
I want to pass Authorization header while POSTing data to server. I tried
$.ajax({
url : <ServiceURL>,
data : JSON.stringify(JSonData),
type : 'POST',
contentType : "text/html",
dataType : 'json',
success : function(Result) {
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', <Authorization Header Value>);
},
error: function (RcvData, error) {
console.log(RcvData);
}
});
But REST service returns error (error code : 500). The same service was working fine with $.post() before adding authorization. could anyone tell me "How to pass authorization header in $.post()??"
I want to pass Authorization header while POSTing data to server. I tried
$.ajax({
url : <ServiceURL>,
data : JSON.stringify(JSonData),
type : 'POST',
contentType : "text/html",
dataType : 'json',
success : function(Result) {
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', <Authorization Header Value>);
},
error: function (RcvData, error) {
console.log(RcvData);
}
});
But REST service returns error (error code : 500). The same service was working fine with $.post() before adding authorization. could anyone tell me "How to pass authorization header in $.post()??"
Share Improve this question asked Jan 22, 2014 at 6:56 Umesh KadamUmesh Kadam 8832 gold badges7 silver badges14 bronze badges2 Answers
Reset to default 4Use
contentType: 'application/json',
You may have gotten data
and contentType
mixed up.
contentType
is theContent-type
header you send.data
changes how jQuery treats the data you receive.
The jQuery $.ajax() method accepts a headers
value in the settings object.
So:
$.ajax({
// url, data, etc...
headers: {
"Authorization" :"Basic " + myBase64variable,
"Content-Type" :"application/json"
}
});
Source: http://api.jquery./jquery.ajax/
PS: Seems you can also pass in a new settings object in the beforeSend parameter. I didn't know this, so thanks for asking this question :)
本文标签: restHow to pass authorization header in post() method using JavascriptStack Overflow
版权声明:本文标题:rest - How to pass authorization header in $.post() method using Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741241312a2364034.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论