前端代码如下
x.withCredentials =true
x.open('GET', 'http://www.example.com/xxx');
x.setRequestHeader("lang", "zh");
x.onload=function(){
console.log(x.responseText);
};
x.send();
响应头:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: x-requested-with, Content-Type,Cookie, Accept, multipart/form-data, application/x-www-form-urlencoded, lang, token, os, version, time
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin: http://192.168.0.128:8020
Cache-Control: no-cache, must-revalidate, no-cache, private
Connection: close
Content-Type: application/json
Date: Thu, 06 Sep 2018 10:27:57 GMT
Pragma: no-cache
Proxy-Connection: keep-alive
Server: nginx/1.13.12
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Powered-By: PHP/7.2.5
请求头:
lang: zh
Origin: http://192.168.0.128:8020
Referer: http://192.168.0.128:8020/test/new_file.html?__hbt=1536228082860
User-Agent: Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Mobile Safari/537.36
后端是有 cookie 返回的,直接在浏览器输入接口 url 可以看到 cookie,而且后端也设置了 Access-Control-Allow-Credentials: true,可以在响应头看到,在网山查了很久,找不出是什么地方的问题,求大佬指点!
1
Sparetire 2018-09-06 19:52:42 +08:00 via Android
httponly ?
|
3
KuroNekoFan 2018-09-06 20:10:17 +08:00 via iPhone
我觉得可以先搞搞明白同源策略和 cors 的一些细节
|
4
conn4575 2018-09-06 21:11:03 +08:00 via Android
看是不是 cookie 得 domain 设错了
|
5
zhengxiaowai 2018-09-06 22:50:19 +08:00
cookie 本身就不能跨域。。
|
6
duan602728596 2018-09-06 22:59:50 +08:00 via iPhone
不能跨域不是很正常的吗?
|
7
lucky2javascript 2018-09-07 01:24:20 +08:00
跨域怎么共享?
|
8
Sparetire 2018-09-07 01:53:05 +08:00 via Android
楼上各位 cookie 语境下的域和同源策略语境下跨域的域不是一个概念吧。。同源策略下同域名不同端口就算跨域,cookie 这种情况显然可以共享。。同源策略下子域 xhr 请求就跨域了,cookie 也可以共享,所以难道不是应该看看是否允许子域共享,path 是否正确,是否 httpinly
|
10
lucky2javascript 2018-09-07 01:55:29 +08:00
|
11
Sparetire 2018-09-07 02:00:52 +08:00 via Android
|
12
w3313003 2018-09-07 09:13:47 +08:00
前端也要 x.withCredentials = true
|
13
laoona 2018-09-07 15:27:42 +08:00
跨域请求可以携带 cookie (后端接口能收到),但后端接口在浏览器下是种植不了 cookie 的。
|
14
cfort OP 感谢各位,开发环境下把接口和项目域名设置一样暂时避开了这个问题~
|