首页 > 解决方案 > 如何将此获取响应转换为字符串?

问题描述

我有一个网址https://blah/configs.ini

它基本上看起来像这样:

[cdmglobal]
collectionHomepageImage = "upload"
customLandingPageAltText = ""
customLandingPageImage = "images/landingpage/small-monographiiif2.jpg
...
...

我需要那个customLandingPageImage 的名称。我的计划是将它放入一个字符串中,然后在 '/images/landingpage' 之后抓取该位,直到我到达 '.jpg'

我试过这个

let newSource = "http://blah/configs.ini";
fetch(newSource)
.then(function(response) {
    let responseString = response.text();
    console.log(responseString);
}).catch(function() {
    console.log("error");
});

现在在 Chrome 开发工具中,它向我展示了:

Promise
    __proto__: Promise
        [[PromiseStatus]]: "resolved"
        [[PromiseValue]]: "[cdmglobal]↵collectionHomepageImage = "upload"↵customLandingPageAltText = ""↵customLandingPageImage = "images/landingpage/small-monographiiif2.jpg

我真的只是想要那个 promiseValue 但它只以承诺值的形式返回它,而不是字符串。

标签: javascriptfetch

解决方案


这应该有效;

let newSource = "http://blah/configs.ini";
fetch(newSource)
.then(function(response) {
    response.text().then(function(responseString) { 
        console.log(responseString);
    });
}).catch(function() {
    console.log("error");
});

推荐阅读