Fetch 没有设置 cookie
Fetch isn't setting cookies
我在根 /
上有一个 reactjs 前端 运行。
在同一域和服务器上,默认 Wordpress slugs(/wp-admin
、/wp-json
等)下有一个 Wordpress 实例 运行。我已经通过 WP API.
通过 JWT 对用户进行了身份验证
问题 是我需要获取 Wordpress 默认身份验证 cookie,以便用户无需登录两次即可在 Wordpress 和 reactjs 页面之间切换。
我开始在登录时添加第二次提取,它调用 /login.php
路由并将登录凭据发布为 form-data。在 Postman 中,我得到 body、headers 和我需要的身份验证 cookie:
如果我在我的 React 前端中获取它,我不会得到 cookie,而且在 document.cookie
或开发工具下也没有设置和可用。
这是我在 React 中的提取函数,它将仪表板页面作为响应数据和状态代码 200 返回:
async handleCookies() {
const formData = new FormData()
formData.append('log', this.state.mail)
formData.append('pwd', this.state.password)
await fetch('XXX.XXX.XXX.XXX/wp-login.php', {
method: 'POST',
body: formData,
credentials: 'include',
headers: { 'Content-type': 'application/x-www-form-urlencoded', Cache: 'no-cache' },
})
.then(response => response)
.then(cookies => {
alert(cookies)
})
.catch(err => console.log(err))
}
期望:我希望收到饼干。
原来我没有以正确的方式传递凭据。现在一切正常。
await fetch('XXX.XXX.XXX.XXX/wp-login.php', {
method: 'POST',
body: `log=${encodeURIComponent(EMAIL)}&pwd=${encodeURIComponent(PASSWORD)}`,
credentials: 'include',
headers: { 'Content-type': 'application/x-www-form-urlencoded', Cache: 'no-cache' },
})
我在根 /
上有一个 reactjs 前端 运行。
在同一域和服务器上,默认 Wordpress slugs(/wp-admin
、/wp-json
等)下有一个 Wordpress 实例 运行。我已经通过 WP API.
问题 是我需要获取 Wordpress 默认身份验证 cookie,以便用户无需登录两次即可在 Wordpress 和 reactjs 页面之间切换。
我开始在登录时添加第二次提取,它调用 /login.php
路由并将登录凭据发布为 form-data。在 Postman 中,我得到 body、headers 和我需要的身份验证 cookie:
如果我在我的 React 前端中获取它,我不会得到 cookie,而且在 document.cookie
或开发工具下也没有设置和可用。
这是我在 React 中的提取函数,它将仪表板页面作为响应数据和状态代码 200 返回:
async handleCookies() {
const formData = new FormData()
formData.append('log', this.state.mail)
formData.append('pwd', this.state.password)
await fetch('XXX.XXX.XXX.XXX/wp-login.php', {
method: 'POST',
body: formData,
credentials: 'include',
headers: { 'Content-type': 'application/x-www-form-urlencoded', Cache: 'no-cache' },
})
.then(response => response)
.then(cookies => {
alert(cookies)
})
.catch(err => console.log(err))
}
期望:我希望收到饼干。
原来我没有以正确的方式传递凭据。现在一切正常。
await fetch('XXX.XXX.XXX.XXX/wp-login.php', {
method: 'POST',
body: `log=${encodeURIComponent(EMAIL)}&pwd=${encodeURIComponent(PASSWORD)}`,
credentials: 'include',
headers: { 'Content-type': 'application/x-www-form-urlencoded', Cache: 'no-cache' },
})