【问题标题】:Why do I get error code 401 when sending post request为什么我在发送 post 请求时收到错误代码 401
【发布时间】:2021-03-05 11:06:51
【问题描述】:

从以下请求中,我得到状态代码 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

    }

【问题讨论】:

    标签: javascript cookies request http-headers http-post


    【解决方案1】:

    通常正文数据类型必须与 Content-Type 标头匹配。所以在你的情况下你必须使用

    body: new URLSearchParams({
         'userName': 'name',
          'password': 'pass',
          'grant_type': 'password'
        }),
    

    【讨论】:

    • 我试过了,然后我得到了状态码 403 'forbidden'。
    猜你喜欢
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    相关资源
    最近更新 更多