[返回爱GPT首页]·[所有跟帖]·[ 回复本帖 ] ·[热门原创] ·[繁體閱讀]·[坛主管理]

编程对ChatGPT轻而易举。如何在 Javascript 中发出 HTTP 请求?

送交者: nowhere1[♂☆★★✦娱乐人生✦★★☆♂] 于 2023-03-20 21:23 已读 885 次 2赞  

nowhere1的个人频道

+关注

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朋友的这个贴子的话, 请点这里投票,“赞”助支持!

已标注为nowhere1的原创内容,若需转载授权请联系网友本人。若违规侵权,请联系我们

所有跟帖:   ( 主贴楼主有权删除不文明回复,拉黑不受欢迎的用户 )


用户名: 密码: [--注册ID--]

标 题:

粗体 斜体 下划线 居中 插入图片插入图片 插入Flash插入Flash动画


     图片上传  Youtube代码器  预览辅助



[ 留园条例 ] [ 广告服务 ] [ 联系我们 ] [ 个人帐户 ] [ 创建您的定制新论坛频道 ] [ Contact us ]