Javascript 如何将此获取响应转换为字符串?
我有一个网址 基本上是这样的:Javascript 如何将此获取响应转换为字符串?,javascript,fetch,Javascript,Fetch,我有一个网址 基本上是这样的: [cdmglobal] collectionHomepageImage = "upload" customLandingPageAltText = "" customLandingPageImage = "images/landingpage/small-monographiiif2.jpg ... ... 我需要那个customLandingPageImage的名字。我的计划是把它放进一个字符串中,然后在“/images/landingpage”后面抓住该位,
[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,但它只以promise值的形式返回,而不是以字符串的形式返回
let newSource = "http://blah/configs.ini";
fetch(newSource)
.then(function(response) {
response.text().then(function(responseString) {
console.log(responseString);
});
}).catch(function() {
console.log("error");
});
这应该行得通
let newSource = "http://blah/configs.ini";
fetch(newSource)
.then(function(response) {
response.text().then(function(responseString) {
console.log(responseString);
});
}).catch(function() {
console.log("error");
});
为什么我可以在一个小时内尝试解决这个问题,然后在发布到堆栈溢出后12秒解决它 解决方案:
fetch(newsource)
.then(function(response) {
return response.text();
}).then(function(text) {
console.log(text);
}).catch(function() {
console.log("error");
});
为什么我可以在一个小时内尝试解决这个问题,然后在发布到堆栈溢出后12秒解决它 解决方案:
fetch(newsource)
.then(function(response) {
return response.text();
}).then(function(text) {
console.log(text);
}).catch(function() {
console.log("error");
});
fetch的text/json方法是异步的,并返回一个承诺,因此您必须再次/wait。fetch的text/json方法是异步的,并返回一个承诺,因此您必须再次/wait谢谢。将在可用时选择您的答案!这发生在我们所有人身上!不过我还是建议你,或者至少返回内心的承诺。否则,
catch
处理将无法工作!谢谢你善良的陌生人。将在可用时选择您的答案!这发生在我们所有人身上!不过我还是建议你,或者至少返回内心的承诺。否则,catch
处理将无法工作!在您的代码中,bleh变量是必需的吗?我认为您可以删除它,只需调用console.log(text)就可以了。在您的代码中,变量bleh是必需的吗?我认为您可以删除它,只需调用console.log(text)即可