試圖實施ajax登錄/註冊過程(無需身份驗證的刷新站點)。使用Cookie保存狀態。我認爲我現在有這個權利,但由於某種原因,瀏覽器在從服務器獲取它們之後不會設置cookie。誰能幫忙?下面是請求和響應頭:通過CORS的Ajax請求在瀏覽器上設置Cookie
Request URL:http://api.site.dev/v1/login
Request Method:POST
Status Code:200 OK
請求頭
Accept:application/json, text/plain, */*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:57
Content-Type:application/json;charset=UTF-8
Host:api.site.dev
Origin:http://site.dev
Referer:http://site.dev/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.101 Safari/537.11
withCredentials:true
X-Requested-With:XMLHttpRequest
Request Payload
{"email":"[email protected]","password":"foobar"}
響應頭
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:X-Requested-With, Content-Type, withCredentials
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:http://site.dev
Connection:Keep-Alive
Content-Length:19
Content-Type:application/json
Date:Tue, 08 Jan 2013 18:23:14 GMT
Keep-Alive:timeout=5, max=99
Server:Apache/2.2.22 (Unix) DAV/2 PHP/5.4.7 mod_ssl/2.2.22 OpenSSL/0.9.8r
Set-Cookie:site=%2B1THQQ%2BbZkEwTYFvXFVV5fxi00l2K%2B6fvt9SuHACTNsEwUGzDSUckt38ZeDsNbZSsqzHmPMWRLc84eDLZzh8%2Fw%3D%3D; expires=Thu, 10-Jan-2013 18:23:14 GMT; path=/; domain=.site.dev; httponly
X-Powered-By:PHP/5.4.7
我也看到在Chrome網絡工具的cookie,從返回服務器:
個響應cookie
Name: site
Value: %2B1THQQ%2BbZkEwTYFvXFVV5fxi00l2K%2B6fvt9SuHACTNsEwUGzDSUckt38ZeDsNbZSsqzHmPMWRLc84eDLZzh8%2Fw%3D%3D
Domain: .site.dev
Path:/
Expires: Session
Size: 196
Http: ✓
這是根據RFC2109第4.3.2節的域匹配問題?我並不完全清楚您的主機(api.site.dev)和cookie域(.site.dev)是否根據需要「域匹配」。 CORS方面並沒有使這個更容易演繹:) – broofa
[CORS請求 - 爲什麼cookie沒有發送?](http://stackoverflow.com/questions/8863571/cors-request - 爲什麼是cookies沒有發送) –