Ami*_*mak 2 rest session-cookies cors preflight angularjs-http
AngularJS使用REST对服务器端进行身份验证,并获取JSESSIONID cookie.在下一步中,我尝试使用REST从服务器端获取一些JSON数据以及在上一步中获得的会话cookie.这是客户端代码:
getSomeJSONDataFromServer:function() {
var deferred = $q.defer();
$http({
method: 'POST',
withCredentials: true,
url: "http://domain.name/app/someURL",
headers:{
'Accept':'application/json',
'Content-Type':'application/json; charset=utf-8',
'Access-Control-Request-Headers': 'X-Requested-With, content-type, accept, origin, withcredentials'
}
})
.success(function(data, status, headers, config) {
// handle data
})
.error(function(data, status, headers, config) {
// handle error
});
return deferred.promise;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常:

当我在上面的POST请求体中发送一些数据时,问题就开始了.
...
$http({
method: 'POST',
withCredentials: true,
url: "http://domain.name/app/someURL",
headers:{
'Accept':'application/json',
'Content-Type':'application/json; charset=utf-8',
'Access-Control-Request-Headers': 'X-Requested-With, content-type, accept, origin, withcredentials'
},
data: '{}'
})
.success(...
Run Code Online (Sandbox Code Playgroud)
上述代码在prelight请求中失败:

看起来服务器启动一个新会话,因为会话cookie由于某种原因没有发送.无论如何,我觉得我错过了一些非常简单的东西,一些标题或类似的东西...任何想法都很感激.提前致谢.
小智 7
阿米尔,
请允许我帮助您和其他人清楚地了解处理CORS时的预期,例如:
这个SIMPLE版本显然不是你的情况......
这个PREFLIGHTED版本显然是你的情况......
凭证明确的请求也是您的理由......