0

我正在尝试从我的 API 存储身份验证令牌,因此我可以使用它向另一个 API 发出 GET 请求,我的代码如下:

fetch('APIURL', {
        method: 'POST',
        body: JSON.stringify({
            username: currentUser.username,
            password: currentUser.password,
        }),
    })
        .then(response => console.log(response))
        .then(json => console.log(json))

用户名和密码来自我的表单输入,我已成功发出 POST 请求并在我的浏览器中获取了令牌,但我想将其存储在我的代码中,在此先感谢 :)

4

2 回答 2

0

对于任何想知道的人,我使用带有以下代码的 axios 使其工作:

axios({
        method: "post",
        url: "APIURL",
        data: {
            username: currentUser.username,
            password: currentUser.password
        }
    })
        .then(function (response) {
            const responseStatus = (response.status)
            const authToken = (response.data)

        })

无论如何感谢您的帮助!

于 2020-05-20T20:27:26.373 回答
0

您可以使用 localstorage 设置和获取身份验证令牌。

要将字符串保存在本地存储中,您可以使用

window.localStorage.setItem(key, value);

您可以稍后通过以下方式获取该值:

window.localStorage.getItem(key);
于 2020-05-20T03:14:02.823 回答