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;
}
上面的代码工作正常:
当我在上面的 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(...
上面的代码在 prelight 请求中失败:
看起来服务器启动了一个新会话,因为会话 cookie 由于某种原因没有发送。无论如何,我觉得我错过了一些非常简单的东西,一些标题或类似的东西......任何想法都值得赞赏。提前致谢。