JSON Post with Customized HTTPHeader Field
What you posted has a syntax error, but it makes no difference as you cannot pass HTTP headers via $.post()
.
Provided you're on jQuery version >= 1.5, switch to $.ajax()
and pass the headers
(docs) option. (If you're on an older version of jQuery, I will show you how to do it via the beforeSend
option.)
$.ajax({
url: 'https://url.com',
type: 'post',
data: {
access_token: 'XXXXXXXXXXXXXXXXXXX'
},
headers: {
Header_Name_One: 'Header Value One', //If your header name has spaces or any other char not appropriate
"Header Name Two": 'Header Value Two' //for object property name, use quoted notation shown in second
},
dataType: 'json',
success: function (data) {
console.info(data);
}
});
if one wants to use .post() then this will set headers for all future request made with jquery
$.ajaxSetup({
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
then make your .post() calls as normal.
I tried as you mentioned, but only first parameter is going through and rest all are appearing in the server as undefined
. I am passing JSONWebToken
as part of header.
.ajax({
url: 'api/outletadd',
type: 'post',
data: { outletname:outletname , addressA:addressA , addressB:addressB, city:city , postcode:postcode , state:state , country:country , menuid:menuid },
headers: {
authorization: storedJWT
},
dataType: 'json',
success: function (data){
alert("Outlet Created");
},
error: function (data){
alert("Outlet Creation Failed, please try again.");
}
});