The following request works fine:
curl -X GET --header 'Accept: application/json' --header 'X-Authorization: Bearer eyNI6GJ0DA9ObwNA1QAv3oqN2zWWQHsiAxR3m-QZjtaw' 'http://host:port/api/tenant/devices?limit=10'
Now I need to perform the same request through jquery.ajax()
.
I have tried this:
var mytoken = "Bearer eyNI6GJ0DA9ObwNA1QAv3oqN2zWWQHsiAxR3m-QZjtaw ";
$.ajax({
type: 'GET',
url: "http://" + host + ":" + port + "/api/tenant/devices?limit=10",
contentType: "application/json",
dataType: "JSON",
xhrFields: {
withCredentials: true
},
beforeSend: function(xhr) {
console.log("beforesend");
xhr.setRequestHeader ("X-Authorization", mytoken);
},
success:function(data) {
console.log(data);
},
error:function(error){
console.log(error);
}
});
What I obtained is:
Response: {"status":401,"message":"Authentication failed","errorCode":10,"timestamp":1518184124772}
Console:
Failed to load http://host:port/api/tenant/devices?limit=10: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 401.
Which is the right way to traslate the working curl rest to jquery ajax?