以下请求正常:
curl -X GET --header 'Accept: application/json' --header 'X-Authorization: Bearer eyNI6GJ0DA9ObwNA1QAv3oqN2zWWQHsiAxR3m-QZjtaw' 'http://host:port/api/tenant/devices?limit=10'
现在我需要通过jquery.ajax()
执行相同的请求。
我试过这个:
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);
}
});
我获得的是:
Response: {"status":401,"message":"Authentication failed","errorCode":10,"timestamp":1518184124772}
控制台:
无法加载http://host:port/api/tenant/devices?limit=10:对预检请求的响应未通过访问控制检查:请求的资源上没有“Access-Control-Allow-Origin”标头。因此不允许原点'null'访问。响应的HTTP状态代码为401。
将工作curl rest转换为jquery ajax的正确方法是什么?