HTTP 之 请求方法(三)

1. GET:请求指定的资源。

GET 请求应该只用于获取数据,而不会导致服务器上的状态变化。

javascript 复制代码
//通常用于请求页面或数据。
fetch('http://www.example.com/data')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

2. POST:向服务器提交新的资源。

POST 请求通常用于表单提交或上传文件,数据在请求体中。

javascript 复制代码
//提交用户注册信息。
fetch('http://www.example.com/register', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    username: 'newuser',
    password: 'newpass'
  })
})
.then(response => response.json())
.then(data => console.log('Registered:', data))
.catch(error => console.error('Error:', error));

3. PUT:更新服务器上的现有资源。

如果资源不存在,则可能会创建新资源。PUT 请求是幂等的。

javascript 复制代码
//更新用户的配置信息。
fetch('http://www.example.com/settings', {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    theme: 'dark',
    notifications: true
  })
})
.then(response => response.json())
.then(data => console.log('Settings Updated:', data))
.catch(error => console.error('Error:', error));

4. DELETE:从服务器删除指定的资源。

DELETE 请求通常用于删除资源。

javascript 复制代码
//删除用户的账户。
fetch('http://www.example.com/users/123', {
  method: 'DELETE'
})
.then(response => response.json())
.then(data => console.log('Delete Response:', data))
.catch(error => console.error('Error:', error));

5. HEAD:请求资源的响应头信息,不返回响应体。

HEAD 请求与 GET 类似,但不包括响应体。

javascript 复制代码
//检查资源是否存在或获取资源的元数据。
fetch('http://www.example.com/image.png', {
  method: 'HEAD'
})
.then(response => {
  console.log(`Content-Type: ${response.headers.get('Content-Type')}`);
})
.catch(error => console.error('Error:', error));

6. OPTIONS:描述目标资源的通信选项。

OPTIONS 请求通常用于跨域资源共享(CORS)的预检请求。

javascript 复制代码
//获取服务器支持的 HTTP 方法。
fetch('http://www.example.com/api/resource', {
  method: 'OPTIONS'
})
.then(response => response.json())
.then(data => console.log('Allowed Methods:', data))
.catch(error => console.error('Error:', error));

7. TRACE:沿着到目标资源的路径执行一个消息环回测试。

javascript 复制代码
TRACE 请求通常用于诊断。
javascript 复制代码
fetch('http://www.example.com/trace', {
  method: 'TRACE'
})
.then(response => response.text())
.then(data => console.log('Trace Response:', data))
.catch(error => console.error('Error:', error));

8. CONNECT:建立一个到服务器的隧道,通常用于 SSL 加密的代理请求。

不常用于前端开发中,主要用于通过代理服务器建立安全连接。

通常不会在前端代码中看到 CONNECT 方法的使用。

9. PATCH:对资源进行部分更新。

PATCH 请求允许只发送需要更新的部分数据。

javascript 复制代码
fetch('http://www.example.com/users/123', {
  method: 'PATCH',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    email: 'newemail@example.com'
  })
})
.then(response => response.json())
.then(data => console.log('User Updated:', data))
.catch(error => console.error('Error:', error));
相关推荐
kyriewen9 分钟前
我把 7 个 UI 库的下拉框用键盘敲了一遍,3 家出了岔子
前端·javascript·程序员
陈随易10 分钟前
MoonBit 2026 深圳线下交流分享
前端·后端·程序员
玖米GEO运营王总监17 分钟前
抖音和视频号算法区别:玖米快跑的双端分发信号与内容适配对比
java·前端·人工智能
鼓掌MVP21 分钟前
华为云码道实战:用 CodeArts Agent + 智能化搜索工具 API 调用自主开发「非遗文旅宣传卡生成器」
前端·人工智能·ai·html·ai编程·ai智能体·ai自动化
不要试图纠正别人1 小时前
1055 个文件,共享 0 字节:pnpm 硬链接的静默降级
前端·node.js
老秦包你会1 小时前
计算机网络第四课 ----Socket(套接字)编程TCP
网络协议·tcp/ip·计算机网络
tryCbest1 小时前
Vue系列——第 24 篇:VueUse——组合式工具集的百宝箱
前端·vue.js
GuWenyue1 小时前
setInterval 里的 state 永远是 0?我把 React 闭包陷阱的 5 个反例全写了一遍
前端·react.js
Xeni1 小时前
从「会写」到「真懂」:SSE 流式输出的 20 行代码与 7 个坑
前端
bai2731 小时前
字段一个不差,md5 却对不上:对象键顺序的三条规则
前端·javascript