编程对ChatGPT轻而易举。如何在 Javascript 中发出 HTTP 请求?
How do I make an HTTP request in Javascript? You can make an HTTP request in JavaScript using the fetch API or the XMLHttpRequest object.
Using fetch API:
javascriptCopy code
fetch(url)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));Here, url is the URL of the resource you want to fetch. You can replace .json() with .text() or .blob() depending on the type of response you expect.
Using XMLHttpRequest object:
javascriptCopy code
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.0nload = function() {
if (xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.error(xhr.statusText);
}
};
xhr.0nerror = function() {
console.error('An error occurred.');
};
xhr.send();Here, url is the URL of the resource you want to fetch. You can replace GET with POST or any other HTTP method depending on the type of request you want to make.
贴主:nowhere1于2023_03_20 22:08:16编辑
已标注为nowhere1的原创内容,若需转载授权请联系网友本人。若违规侵权,请联系我们
所有跟帖: ( 主贴楼主有权删除不文明回复,拉黑不受欢迎的用户 )
楼主前期社区热帖:
>>>>查看更多楼主社区动态...