# 跨域請求如何攜帶 Cookie

### 在前端請求的時候設置 request 對象屬性 withCredentials 為 true

什麼是 `withCredentials`?

`withCredentials` 是 `XMLHttpRequest` 中的一個 `Boolean` 類型，

只是是否使用 cookies, authorized header（頭部授權） 或者是 TSL 客戶端證書來建立跨域請求，同一個站下使用 withCredentials 是無效的

如果發送來自其他網域的 `withCredentials`，沒有設置 true 的話，就不能為他自己的網域設置 cookie。設置 true 獲得的第三方 cookie，依舊會享受同源政策。

另外也需要請服服務端 response header 設定 `Access-Control-Allow-Origin` 和 `Access-Control-Allow-Credentials`。
