Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用XMLHttpRequest进行302重定向后的Cookie管理_Javascript_Cookies_Xmlhttprequest_Firefox Os_Http Status Code 302 - Fatal编程技术网

Javascript 使用XMLHttpRequest进行302重定向后的Cookie管理

Javascript 使用XMLHttpRequest进行302重定向后的Cookie管理,javascript,cookies,xmlhttprequest,firefox-os,http-status-code-302,Javascript,Cookies,Xmlhttprequest,Firefox Os,Http Status Code 302,我正在为ownCloud开发Firefox操作系统客户端。当我尝试登录并向服务器发送用户凭据时,我需要获取cookie作为响应,我将在每个请求中使用cookie在ownCloud中进行身份验证 我的问题是,正如我在Wireshark中看到的,cookie是以HTTP 302消息的形式发送的,但我无法在代码中读取此消息,因为Firefox会自动处理此消息,而我读取的最终HTTP 200消息中没有cookie信息 request.reponseText; request.getAllRespons

我正在为ownCloud开发Firefox操作系统客户端。当我尝试登录并向服务器发送用户凭据时,我需要获取cookie作为响应,我将在每个请求中使用cookie在ownCloud中进行身份验证

我的问题是,正如我在Wireshark中看到的,cookie是以HTTP 302消息的形式发送的,但我无法在代码中读取此消息,因为Firefox会自动处理此消息,而我读取的最终HTTP 200消息中没有cookie信息

request.reponseText; 
request.getAllResponseHeaders();
所以我的问题是,是否有任何方法可以读取这个HTTP302消息头,或者我是否可以在发送下一个请求之前从Firefox OS获得cookie,或者甚至让Firefox OS自动添加cookie。我使用以下代码来制作帖子:

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";
}
“莫扎农

布尔值:将此标志设置为true将导致浏览器在获取资源时不公开源凭据和用户凭据。最重要的是,这意味着除非使用setRequestHeader显式添加Cookie,否则不会发送Cookie

莫扎特系统

布尔值:将此标志设置为true允许进行跨站点连接,而无需服务器选择使用CORS。需要将mozAnon:true设置为true,即这不能与发送Cookie或其他用户凭据结合使用。“[0]

我不确定您是否是自己的云开发者,但如果您是并且有权访问服务器,您应该尝试设置CORS头。[1] 也许你可以建立一个代理服务器,让你的应用程序连接到启用CORS的代理服务器上

您还可以在xhr对象的实例上设置withCredentials属性[2]。看起来它将添加头
访问控制请求头:“cookies”
并发送HTTP选项请求,这是飞行前[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();
如果响应头存在(并非每个站点都会在每个请求上设置cookie),您可能希望在onload事件中重新分配document.cookie,而不是记录它。您还需要将请求头设置为document.cookie本身

[0]

[1]

[2]

[3]

[4]

[5]