如果URL不正确,则javascript

问题描述 投票:0回答:1

window.onload = () => { fetch("https://www.w3schools.com/nodejs/incorrecturl") .then(res => res.json()) .then(data => console.log(data)) .catch(error => alert(error.toString())) // Here i need proper error message, instead of "failed to fetch". }

trone这个
var myRequest = new Request('https://www.w3schools.com/nodejs/incorrecturl');
fetch(myRequest).then(function(response) {
    console.log(response.status);
});

fetch('https://www.w3schools.com/nodejs/incorrecturl').then(function(response) { console.log(response.status); });
javascript promise try-catch fetch
1个回答
1
投票
refs:

https://developer.mozilla.org/en-us/docs/web/api/response/status

    

您需要捕获错误
fetch('https://www.w3schools.com/nodejs/incorrecturl')
 .then((response) => { console.log(response.status); })
 .catch((error) => { console.error('Request failed:', error); });

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.