鍍金池/ 問答/Linux  網(wǎng)絡(luò)安全  HTML/ Nginx如何配置跨域請求時(shí)包含cookie

Nginx如何配置跨域請求時(shí)包含cookie

前端: front.foo.com
后端: back.foo.com

當(dāng)前端通過ajax調(diào)用后端接口的時(shí)候 如果想把cookie傳給后端 需要顯式設(shè)置:

xhrFields: { withCredentials: true },

如下所示

$.ajax({
type: “post”,
url: ‘http://back.foo.com/search’,
contentType : “application/json;charset=utf-8”,
dataType: “json”,
xhrFields: { withCredentials: true },
...

不知道前端能不能不用顯式做此設(shè)置 直接通過Nginx來實(shí)現(xiàn)此目的 如在請求頭中動態(tài)的添加:withCredentials: true

回答
編輯回答
伐木累
不知道前端能不能不用顯式做此設(shè)置 直接通過Nginx來實(shí)現(xiàn)此目的

不能。 cookie 是瀏覽器的東西,又不是 nginx 的東西。

2017年4月3日 02:23