XMLHttpRequest: withCredentials プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

XMLHttpRequest.withCredentials プロパティは論理値で、サイト間の Access-Control リクエストが Cookie、認証ヘッダー、 TLS クライアント証明書などの資格情報を使用して行うべきかどうかを示します。 withCredentials を設定しても、同じオリジンへのリクエストには影響しません。

さらに、このフラグは、レスポンスにおいて Cookie を無視すること示すためにも使われます。既定値は false です。異なるドメインからの XMLHttpRequest のレスポンスは、リクエストを行う前に withCredentialstrue に設定しない限り、自身のドメインの Cookie 値を設定することができません。 withCredentials を true に設定することで得られるサードパーティの Cookie は、依然として same-origin ポリシーを尊重するので、リクエストスクリプトが document.cookie やレスポンスヘッダーからアクセスすることはできません。

メモ: これは同じオリジンへのリクエストには影響しません。

メモ: 異なるドメインからの XMLHttpRequest レスポンスは、リクエストを行う前に withCredentialstrue に設定しない限り、 Access-Control- ヘッダーの値にかかわらず、自ドメインの Cookie 値を設定できません

論理値です。

js
const xhr = new XMLHttpRequest();
xhr.open("GET", "http://example.com/", true);
xhr.withCredentials = true;
xhr.send(null);

仕様書

Specification
XMLHttpRequest
# the-withcredentials-attribute

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
withCredentials

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.