0

以下请求工作正常:

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”标头. 因此,Origin 'null' 不允许访问。响应具有 HTTP 状态代码 401。

将工作 curl rest 转换为 jquery ajax 的正确方法是什么?

4

0 回答 0