When I make my get request with POSTMAN, it works just fine and I get the expected response. I just set type to GET, insert my URL and insert a key named token and set it to a token I get from another service. but when I try to call the same service in code like this:
$.ajax({
type: 'get',
headers: {"token": myToken},
url: myURL,
success: function (data) {
console.log(data);
}
});
or this:
$.ajax({
type: 'GET',
beforeSend: function(xhr, setting){xhr.setRequestHeader('token', myToken);},
url: myURL,
success: function (data) {
console.log(data);
}
});
I get 401 error. I also found out that the server is not receiving the token in the request header. Any ideas how I can fix this? Cheers.
=====================================================
UPDATE: POSTMAN generated code which fails as well:
var settings = {
"async": true,
"crossDomain": true,
"url": "https://test.mirasnafis.ir/marketer/rating",
"method": "GET",
"headers": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IjEwY2U5NTUxLWExNTItNDc2ZC05ZTA0LTIyOGMyOGUwMmEwYSIsIm5iZiI6MTU2NTE1NDU0NywiZXhwIjoxNTk2Nzc2OTQ3LCJpYXQiOjE1NjUxNTQ1NDcsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTAxOTEiLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjUwMTkxIn0.YhdwgSW1NzHavdtXTMlHOvbOmgjlUXk81PKGg4v0cXc",
"User-Agent": "PostmanRuntime/7.15.2",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Postman-Token": "c7e92763-dc2e-4db8-a442-c5b8275e7bf5,d6fa9ad9-29e2-482e-9c46-de89f9a42c3b",
"Host": "test.mirasnafis.ir",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"cache-control": "no-cache"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
and
var data = null;
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("GET", "https://test.mirasnafis.ir/marketer/rating");
xhr.setRequestHeader("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IjEwY2U5NTUxLWExNTItNDc2ZC05ZTA0LTIyOGMyOGUwMmEwYSIsIm5iZiI6MTU2NTE1NDU0NywiZXhwIjoxNTk2Nzc2OTQ3LCJpYXQiOjE1NjUxNTQ1NDcsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTAxOTEiLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjUwMTkxIn0.YhdwgSW1NzHavdtXTMlHOvbOmgjlUXk81PKGg4v0cXc");
xhr.setRequestHeader("User-Agent", "PostmanRuntime/7.15.2");
xhr.setRequestHeader("Accept", "*/*");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("Postman-Token", "c7e92763-dc2e-4db8-a442-c5b8275e7bf5,8da9d6a1-0670-41f5-8049-3b96a28b0e3f");
xhr.setRequestHeader("Host", "test.mirasnafis.ir");
xhr.setRequestHeader("Accept-Encoding", "gzip, deflate");
xhr.setRequestHeader("Connection", "keep-alive");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.send(data);