我知道这是一个新手问题,但我是学习 Ajax 的新手。我正在阅读一些代码并遇到了这一行
xhrFields: {withCredentials: true}
我能够遵循其余的代码;然而,这部分有点困惑。您是否使用它来覆盖凭据?
在documentation它说了以下内容:
xhrFields
Blockquote
Type: PlainObject
An object of fieldName-fieldValue pairs to set on the native XHR
object. For example, you can use it to set withCredentials to true for
cross-domain requests if needed.
我有点明白它在说什么。但是,我不确定它指的是哪种跨域请求。
再次抱歉问了这个菜鸟问题。请不要对我的问题 D 投反对票:
祝你有美好的一天:)!
根据 Mozilla 文档:
The XMLHttpRequest.withCredentials property is a Boolean that
indicates whether or not cross-site Access-Control requests should be
made using credentials such as cookies, authorization headers or TLS
client certificates. Setting withCredentials has no effect on
same-site requests
根据 CORS 规范,cookie 不会被发送,但是当您设置 XMLHttpRequest.withCredentials = true
时,cookie 将被发送到在不同域中运行的服务器。通常,如果服务器 API 位于不同的域中,则不会发送 cookie。
为同源请求设置此属性时无效。但为了使其正常工作,服务器还必须通过将 Access-Control-Allow-Credentials
响应 header 设置为 true
来启用凭据。
引用 Mozilla 文档:
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials