过滤 axios 响应

filtering an axios response

我发出了 axios 请求,我收到了大量返回的数据,但我不确定如何过滤它。

这是我的代码

await axios(config)
.then(function (response) {
const authToken = response
console.log(authToken)  
})
.catch(function (error) {
  console.log(error);
});

这只是响应的一小部分

<form class="edit_checkout" action="/942252/checkouts/536693bdcfc6f7344d1a5b500b546824" accept-charset="UTF-8" method="post"><input type="hidden" name="_method" value="patch" /><input type="hidden" name="authenticity_token" value="hFK7eHqqzlwcmpu2o3eeqVsqiNzruv8w0IPsDUQdDExwN-cdMlEsEWqYGbabkMvUf_rs_FnmmzY6H1MiSdIMOw" />\n' +
    '  <input type="hidden" name="step" value="contact_information" />\n' 

我希望通过 class 形式找到这部分响应,并且我需要与值属性关联的字符串。我该怎么做?

使用 DOMParser 解析响应 - 然后您可以使用 querySelector

await axios(config)
.then(function(response) {
    const parser = new DOMParser;
    const doc = parser.parseFromString(response, "text/html");
    const input = doc.querySelector('form.edit_checkout input[name="authenticity_token"]');
    const value = input.value;
    console.log(value);
})
.catch(function(error) {
    console.log(error);
});

不过,由于您使用的是 await ...

const response = await axios(config);
const parser = new DOMParser;
const doc = parser.parseFromString(response, "text/html");
const input = doc.querySelector('form.edit_checkout input[name="authenticity_token"]');
const value = input.value;
console.log(value);

在try/catch中是更好的代码

顺便说一句,根据 HTML,querySelector 可能只是

doc.querySelector('[name="authenticity_token"]');

我尽可能多地在选择器中添加“自我文档”