javascript - 为什么我在发送 post 请求时收到错误代码 401
问题描述
从以下请求中,我得到状态代码 302 重定向。但我想发送从响应中收到的 cookie,并重定向到下一页。现在我在发送请求时收到状态码 401,我发现这是因为我需要将 cookie 与重定向一起发送,但是在我获取给我重定向的 url 之前我没有得到 cookie . 我怎么做?
async function login (url) {
let request = await fetch(url, {
method: 'POST',
credentials: 'include',
headers: { 'Content-Type' : 'application/x-www-form-urlencoded' },
body: 'username=name&password=pass&submit=login',
})
return request
}
解决方案
一般来说,正文数据类型必须与 Content-Type 标头匹配。所以在你的情况下你必须使用
body: new URLSearchParams({
'userName': 'name',
'password': 'pass',
'grant_type': 'password'
}),
推荐阅读
- visual-studio - 如何在 Visual Studio 2017 的属性表中设置 stdcpp+17?
- python - 在使用 numpy.linalg.eigh() 和 torch.symeig() 之间获取不同的特征值
- javascript - 为 .execute() 设置休息参数
- javascript - 将前端部署到heroku的问题
- swift - 在单元格内输入 NSTextField 时,NSTableView 不会更新行高
- python - 使用路径作为输入而不是文件名?
- python - Python-VLC 在随机播放后更新下一个播放曲目
- c - 为什么我无法初始化 SDL_image?
- css - 自托管字体未在应用程序中加载。我在应用服务器中使用@font-face 和自托管字体
- java - 如何在循环中覆盖字符串