Javascript 在同一来源上进行XMLHttpRequest时,有没有办法不发送cookie?
我正在开发一个为用户解析gmail rss提要的扩展。如果用户不想继续登录,我允许他们指定用户名/密码。但是,如果用户已登录,并且提供的用户名/密码用于其他帐户,则多次登录会中断。因此,我希望避免发送任何cookie,但仍然能够在send()调用中发送用户名/密码。您可以使用。这样做的目的是获取当前cookie,保存它们,从浏览器的cookie存储中删除它们,发送请求,最后恢复它们:Javascript 在同一来源上进行XMLHttpRequest时,有没有办法不发送cookie?,javascript,ajax,google-chrome-extension,xmlhttprequest,Javascript,Ajax,Google Chrome Extension,Xmlhttprequest,我正在开发一个为用户解析gmail rss提要的扩展。如果用户不想继续登录,我允许他们指定用户名/密码。但是,如果用户已登录,并且提供的用户名/密码用于其他帐户,则多次登录会中断。因此,我希望避免发送任何cookie,但仍然能够在send()调用中发送用户名/密码。您可以使用。这样做的目的是获取当前cookie,保存它们,从浏览器的cookie存储中删除它们,发送请求,最后恢复它们: var cookies_temp = []; // where you put the cookies firs
var cookies_temp = []; // where you put the cookies first
var my_cookie_store = []; // the cookies will be there during the request
var details = {/*your code*/}; // the first parameter for chrome.cookies.getAll()
var start_kidnapping = function(cookies) {
cookies_temp = cookies.slice();
kidnap_cookie();
};
var kidnap_cookie = function() {
// This recursive function will store the cookies from cookies_temp to
// my_cookie_store and then remove them from the browser's cookie store.
if (cookies_temp.length == 0) { // when no more cookies, end recursion
send_request();
};
else {
var cookie = cookies_temp.pop();
// We store url as a property since it is useful later.
// You may want to change the scheme.
cookie.url = "http://" + cookie.domain + cookie.path;
my_cookie_store.push(cookie); // save it
chrome.cookies.remove({url: cookie.url, name: cookie.name}, kidnap_cookie);
};
};
var send_request = function() {
// Send your request here. It can be asynchronous.
for (var i = 0, i < my_cookie_store.length; i++){
delete cookie.hostOnly; // these 2 properties are not part of the
delete cookie.session; // object required by chrome.cookies.set()
// note that at this point, cookie is no longer a Cookie object
chrome.cookies.set(my_cookie_store[i]); // restore cookie
};
my_cookie_store = []; // empty it for new adventures
};
chrome.cookies.getAll(details, start_kidnapping); // start
从Chrome 42开始,允许Chrome扩展(以及一般的web应用程序)执行无cookie请求
关于fetch
的高级文档目前非常稀少,但这是一个很好的起点。界面下面描述的获取算法显示,fetch
生成的请求默认没有凭证
fetch('http://example.com/,然后(函数(响应){
return response.text();//是否应该删除cookie.hostOnly;
和删除cookie.session;
行实际上是删除我的cookie存储[i]。hostOnly;
和删除我的cookie存储[i]。session;
?
var incognito_window = {
"url": "incognito.html",
"focused": false, // do not bother user
"incognito": true
}
chrome.windows.create(incognito_window);