fetch请求(请求后端数据的一种方法)

前端fetch方法是用于发送请求的方法。可以很方便的获取到后端数据。

一、发起get请求

fetch(url)
//默认返回response,response下有一个json方法可供使用
  .then(response => response.json())
  .then(data => {
    // 处理返回的数据
  })
  .catch(error => {
    // 处理请求错误
  });

二、发起post请求

const data = { /* 请求体数据 */ };

fetch(url, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json' // 根据实际情况设置请求头
  },
  body: JSON.stringify(data)
})
  .then(response => response.json())
  .then(data => {
    // 处理返回的数据
  })
  .catch(error => {
    // 处理请求错误
  });

三、处理请求头

const headers = new Headers();
headers.append('Authorization', 'Bearer token');

fetch(url, {
  headers: headers
})
  .then(response => response.json())
  .then(data => {
    // 处理返回的数据
  })
  .catch(error => {
    // 处理请求错误
  });

四、处理响应状态。(可以根据响应的状态码来处理不同的情况)

fetch(url)
  .then(response => {
    if (response.ok) {
      // 请求成功
      return response.json();
    } else {
      // 请求失败
      throw new Error('Request failed with status ' + response.status);
    }
  })
  .then(data => {
    // 处理返回的数据
  })
  .catch(error => {
    // 处理请求错误
  });

五、处理跨域

fetch(url, {
  mode: 'cors',
  credentials: 'include' // 发送和接收包含凭据的请求,比如cookie
})
  .then(response => response.json())
  .then(data => {
    // 处理返回的数据
  })
  .catch(error => {
    // 处理请求错误
  });

六、取消请求:
使用AbortControllerAbortSignal可以取消fetch请求。

const controller = new AbortController();
const signal = controller.signal;

fetch(url, { signal })
  .then(response => response.json())
  .then(data => {
    // 处理返回的数据
  })
  .catch(error => {
    if (error.name === 'AbortError') {
      // 请求被取消
    } else {
      // 处理其他请求错误
    }
  });

// 取消请求
controller.abort();

实际开发过程中,建议使用async/awaitaxios简化请求代码

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
前端中处理后端返回的数据可以使用以下方法: 1. 使用原生 JavaScript:使用 XMLHttpRequest 对象发送 POST 请求,并使用回调函数处理返回的数据。示例代码如下: ```javascript var xhr = new XMLHttpRequest(); xhr.open("POST", "backend_url", true); xhr.setRequestHeader("Content-Type", "application/json"); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { var response = JSON.parse(xhr.responseText); // 处理返回的数据 } }; var data = { // 请求数据 }; xhr.send(JSON.stringify(data)); ``` 2. 使用 Fetch API:Fetch API 是一种现代的 JavaScript API,用于发送网络请求。可以使用 fetch 函数发送 POST 请求,并使用 Promise 处理返回的数据。示例代码如下: ```javascript var data = { // 请求数据 }; fetch("backend_url", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(data), }) .then(function (response) { return response.json(); }) .then(function (data) { // 处理返回的数据 }) .catch(function (error) { console.error("Error:", error); }); ``` 3. 使用第三方库:如果你使用了前端框架或库,如 React、Angular、Vue.js 等,它们通常提供了更方便的方法来处理后端返回的数据。你可以查阅对应框架或库的文档,了解如何发送 POST 请求和处理返回的数据。 以上是一些常用的前端处理后端返回数据方法,你可以根据自己的需求选择其中之一进行使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值