av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

node.js - koa 無法將cookie寫入到客戶端

瀏覽:115日期:2024-07-30 14:25:05

問題描述

登錄接口在 api.site.com 下,登錄之后會把用戶的access_token 以cookie 的方式往 site.com 這個域名下寫,但是刷新頁面之后在請求頭里面看不到cookie,this.cookies.get()也是undefined,說明沒有寫成功。

使用的是本地開發環境,api.site.com 和 www.site.com 實際上都是 localhost(127.0.0.1),改的host實現的

代碼如下:

if(validateEmail && validateUsername) { let [User] = yield this.db.query(sql, user); let id = User.id const token = yield user.generateAccessToken(id); this.cookies.set(’access_token’, token, {domain: ’.site.com’ }) this.body = User;}

求大神支招

更新

整了個 nginx 代理,問題依然存在

可以在response header 里面看到服務器端有寫入的 set-cookiesnode.js - koa 無法將cookie寫入到客戶端

但是在瀏覽器的cookies 里面看不到,頁面請求也沒有攜帶這個cookie

問題解答

回答1:

這兩天搜遍了google,so,尼瑪終于找到問題了,覺得非常有必要自己記錄一下。全都是因為這個鬼東西 Request.credentials

The credentials read-only property of the Request interface indicates whether the user agent should send cookies from the other domain in the case of cross-origin requests. This is similar to XHR’s withCredentials flag, but with three available values (instead of two):omit: Never send cookies.same-origin: Only send cookies if the URL is on the same origin as the calling script.include: Always send cookies, even for cross-origin calls.

當我們在發送跨域請求時,request 的 credentials屬性表示是否允許其他域發送cookie,該屬性有3個值:omit: 默認屬性,不允許其他域發送cookiesame-origin: 只允許同域發送cookieinclude: 總是允許發送cookie

所以必須在發送post請求時加上 credentials: include,使用jq的話就是

$.ajax({url: ’http://api.site.com/users’,type: ’POST’,data: postData,xhrFields: { withCredentials: true},success: function (data) { console.log(data)} })

同時在服務端必須加上:

‘Access-Control-Allow-Credentials’:true

這個問題困擾了好多天,還是讀書太少。。。

回答2:

用token

回答3:

先用瀏覽器F12看一下cookies里面確定有了你放的token

回答4:

朋友我跟你同樣的問題,后臺代碼是

this.cookies.set(’c’, ’3’,{domain:’localhost’,httpOnly:true);

前臺同樣是在response Headers 里面能看到Set-Cookies

你是說后臺

this.cookies.set(’c’, ’3’,{domain:’localhost’,httpOnly:true,’Access-Control-Allow-Credentials’:true})

這樣子改嗎?前端請求怎么辦?我的前端請求是這個樣子的:

$.post('http://localhost:8080/user/userlogin?username='+username+'&password='+password,function(data,status){ alert('Data: ' + data.toString()+ 'nStatus: ' + status); let userinfo = data[0]; $('#txtUsername').html(userinfo.username); $('#txtp').show();});

需要怎么調整?

相關文章:
主站蜘蛛池模板: 久久久精品一区二区三区 | 综合久久一区 | 成人h免费观看视频 | 亚洲精品三级 | 日本高清在线一区 | 国产成人免费视频网站高清观看视频 | 久久久久久影院 | 国产精品一区二区视频 | 欧美精品一区二区三区在线 | 欧美日韩中文国产一区发布 | 日韩欧美在线视频观看 | 久久高清| 不卡在线视频 | 成人一区二区在线 | 久久精品日产第一区二区三区 | 少妇精品亚洲一区二区成人 | 国产japanhdxxxx麻豆 | 黄色av大片 | 在线播放日韩 | 久久久精| 91成人免费看 | 国产成人精品久久二区二区91 | 福利视频亚洲 | 91视频在线看 | 久久精品国产精品青草 | 福利国产| 久久精品国产99国产精品亚洲 | 久久久久黑人 | 久久亚洲一区 | 精品二区视频 | 久久新| 伊人国产精品 | 视频一区欧美 | 成人午夜高清 | 天堂一区在线观看 | 欧美激情一区二区三级高清视频 | 久久免费精品 | 日韩欧美在线观看视频 | 精品中文字幕久久 | 国产一区亚洲 | 日日骚网|