我正在为ownCloud开发一个Firefox OS客户端。当我尝试登录并将用户凭据发送到服务器时,我需要在每个请求中获取我将用于在ownCloud中进行身份验证的cookie。
我的问题是,正如我在Wireshark中看到的那样,cookie是在HTTP 302消息中发送的,但是我无法在我的代码中读取此消息,因为Firefox自动处理它并且我读取了最终的HTTP 200消息而没有cookie信息在
request.reponseText;
request.getAllResponseHeaders();
所以我的问题是,如果有任何方法可以阅读此HTTP 302消息标题,或者我是否可以在发送下一个请求之前从Firefox OS获取cookie,或者甚至让Firefox OS自动添加cookie。我使用以下代码进行POST:
request = new XMLHttpRequest({mozSystem: true});
request.open('post', serverInput, true);
request.withCredentials=true;
request.addEventListener('error', onRequestError);
request.setRequestHeader("Cookie",cookie_value);
request.setRequestHeader("Connection","keep-alive");
request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
request.send(send_string);
if(request.status == 200 || request.status==302){
response = request.responseText;
var headers = request.getAllResponseHeaders();
document.getElementById('results').innerHTML="Server found";
loginSuccessfull();
}else{
alert("Response not found");
document.getElementById('results').innerHTML="Server NOT found";
}
答案 0 :(得分:0)
" mozAnon
布尔值:将此标志设置为true将导致浏览器在获取资源时不显示源和用户凭据。最重要的是,这意味着除非使用setRequestHeader明确添加,否则不会发送cookie。
mozSystem
布尔值:将此标志设置为true允许进行跨站点连接,而无需服务器使用CORS选择加入。需要设置mozAnon:true,即这不能与发送cookie或其他用户凭据相结合。" [0]
我不确定您是否是自己的开发人员,但如果您有权访问该服务器,则应尝试设置CORS标头。 [1]也许你可以站起来代理服务器并让你的应用程序连接到启用了CORS的代理服务器?
还可以在xhr对象的实例上设置withCredentials属性[2]。看起来它会添加标题Access-Control-Request-Headers: "cookies"
并发送HTTP OPTIONS请求,这是预检[3]。所以这仍然需要服务器端支持CORS。 [4]
虽然看起来这不应该基于内部评论[5],但我能够从模拟器运行它并查看请求和响应标题:
var x = new XMLHttpRequest({ mozSystem: true });
x.open('get', 'http://stackoverflow.com');
x.onload = function () { console.log(x.getResponseHeader('Set-Cookie')); };
x.setRequestHeader('Cookie', 'hello=world;');
x.send();
您可能希望在onload事件中重新分配document.cookie,而不是记录它,如果响应头存在(并非每个站点都在每个请求上设置cookie)。您还希望将请求标头设置为document.cookie本身。
[0] https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#XMLHttpRequest%28%29
[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
[2] https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#Properties
[3] https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests
[4] http://www.html5rocks.com/en/tutorials/cors/#toc-making-a-cors-request