如何从请求响应中获取单个值



我开始写一个程序,将自动用户操作,到现在它意味着一个更容易的菜单,使更快的行动,只需发送请求到官方网站点击我自己的页面。(有点像网络机器人,但不完全是)。我的问题是当我发送登录请求在响应中,我得到user_id, server,session_id等。我需要保存那个session_id来做其他动作。我如何保存这个变量。

我从昨天开始在网上寻找答案,我仍然找不到(或理解)如何得到这个我试着function login(){ fetch('url', { method: 'POST', headers: { //headers }, body: //my Id's }) })

//这是需要解决的问题.then(res => res.text()) .then(data => obj = data) .then(() => console.log(obj)) console.log(body.session_id);

//我甚至尝试了子字符串but 1。它不会像我想的那样工作,因为有时会有//或多或少的字母。2. 我得到和错误&;Cannot read properties of undefined (reading//'substr')&;

`session = obj;
session_id = session.substring(291,30)
console.log(session_id)`

看起来您正在对从fetch()返回的响应对象使用text()方法,它将为您提供响应的字符串表示。

你可能想要使用json()方法相反,从那你可以session_id

本指南有更多有用的信息,可能会对您有所帮助:https://javascript.info/fetch

现在可以使用

`async function login(){ let session = await fetch('url', {
//code
}
let result = await session.json();
console.log(result);
session_id = result.data.user.session_id;
user_id = result.data.user.id;`

最新更新