I have a problem setting the Authorization header with the $.post
jquery method.
$.post("http://127.0.0.1:8000/newClient/", {
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization','Basic ' + window.localStorage.getItem('token'));
},
data: JSON.stringify(validData)
}, function (response) {
console.log("Esto es el token ",window.localStorage.getItem('token'));
var r = JSON.parse(response);
console.log(r.status);
})
I have the token set correctly and I get it correctly in the response, I have made many AJAX requests with jquery but never with the method $.post
, could it be that this method does not accept the header set?
Error trace.
Uncaught TypeError: Cannot read property 'setRequestHeader' of undefined
I have fixed it by creating a javascript function
beforeSend
.And then setting the ajaxSetup with that function.
So finally the code would look like this, without the header.