我正在访问一个API,我必须首先发布登录信息。下面是我通过什么样的一个例子,我也得到成功的连接:Ajax调用API并传递Set-Cookie
$.ajax({
type: 'POST',
url: 'https://server:port/api/users/_login',
contentType: 'application/x-www-form-urlencoded',
dataType: 'xml',
data: {username: "user", password: "userPassword"},
success: function(data, textStatus, jqXHR){ alert(textStatus); },
error: function(jqXHR, textStatus, errorThrown){ alert(textStatus); }
});
这是200响应:
Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Methods:GET, POST, DELETE, PUT
Access-Control-Allow-Origin:*
Content-Length:125
Content-Type:application/xml
Date:Fri, 21 Oct 2016 08:49:21 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Server:Jetty(9.2.14.v20151106)
Set-Cookie:JSESSIONID=h9823inizvhd2793nuqw7;Path=/api;Secure
任何后续调用必须与JSESSIONID一起做。所以我写了另一个电话如下 - 注释字段将告诉你我已经试过,并打出一轮:
$.ajax({
type: 'GET',
url: 'https://server:port/api/datareq/guid/stats',
dataType: 'xml',
//crossDomain: true,
xhrFields: { withCredentials: true },
//data: data,
success: function(data, textStatus, jqXHR){ alert(textStatus); },
error: function(jqXHR, textStatus, errorThrown){ alert(textStatus);}
});
如果我不包括“withCredentials”(或将其设置为false),我只是得到401:在通话中未经授权。但是,如果我确实使用了该标志,则会收到以下消息:
当凭证标志为true时,通配符'*'不能用于'Access-Control-Allow-Origin'标头中。因此不允许原产地'null'访问。 XMLHttpRequest的凭证模式由withCredentials属性控制。
现在我明白发生了什么,但我无法想象它的方式。阅读相当多的帖子,但没有真正的工作。不幸的是,我无法访问api Web服务器以对该侧进行任何更改,这是一个锁定设备。我将继续关注一些相关文章,但如果有人知道并且可以在解决方法上略微提出一些看法,那么将非常感激。
测试平台:的Windows 8.1
浏览器:的Chrome 53.0.2785.143(正式版本)M(32位)
谢谢,那就来看看返工的方法吧。令人失望:/ – Amble