我正在使用带有 javascript 的 API 进行登录,并且登录正在工作,但是现在,我需要注销,并且我需要从控制台上显示的成功消息中获取令牌,如下所示:
{"access":1,"message":"Success!","status":200,"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjQyLCJ1c2VybmFtZSI6ImJlcm5hcmRvIiwiYWNjZXNzX21vZGUiOjEsImV4cCI6MTY1MzQ3Mjk0NX0.lf6KgIF7FSjKFhPyRvOplYxgq3DLHEbDiRgHhH_oOWY","uid":42,"username":"bernardo"}
但是当我试图像这样在我的 fetch 上获取令牌时:
fetch("https://api.secureme.pt/api/v1/auth/login", requestOptions)
.then((response) => response.text())
.then((result) => console.log(result.token)) <-- HERE
.catch((error) => console.log("error", error));
它返回未定义,我不知道为什么。
在您的代码result
中是 astring
并且您需要的是一个对象。
fetch("https://api.secureme.pt/api/v1/auth/login", requestOptions)
.then((response) => response.json()) // parse JSON data
.then((result) => console.log(result.token))
.catch((error) => console.log("error", error));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句