AJax之Fetch
来源:互联网 发布:小说地图生成软件 编辑:程序博客网 时间:2024/06/03 19:22
简单的fetching示例
在Fetch API中,最常用的就是fetch()函数。它接收一个URL参数,返回一个promise来处理response。response参数带着一个Response对象。
fetch("/data.json").then(function(res) { // res instanceof Response == true. if (res.ok) { res.json().then(function(data) { console.log(data.entries); }); } else { console.log("Looks like the response wasn't perfect, got status", res.status); }}, function(e) { console.log("Fetch failed!", e);});
如果是提交一个POST请求,代码如下:
fetch("http://www.example.org/submit.php", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: "firstName=Nikhil&favColor=blue&password=easytoguess"}).then(function(res) { if (res.ok) { alert("Perfect! Your settings are saved."); } else if (res.status == 401) { alert("Oops! You are not authorized."); }}, function(e) { alert("Error submitting form!");});
<script> var result = fetch('https://api.github.com') result.then(function(response) { console.log('response', response) console.log('header', response.headers.get('Content-Type')) return response.text() }).then(function(text) { console.log('got text', text) }).catch(function(ex) { console.log('failed', ex) }) </script>
兼容浏览器处理
if(self.fetch) { // 使用 fetch 框架处理} else { // 使用 XMLHttpRequest 或者其他封装框架处理}
一般的请求方法
使用 fetch 的构造函数请求数据后,返回一个 Promise 对象,处理即可
fetch("http://blog.parryqiu.com").then(function(response){ // do something...})
0 0
- AJax之Fetch
- Fetch:下一代 Ajax 技术
- Promise,Ajax,fetch
- Fetch-新一代Ajax API
- fetch简介: 新一代Ajax API
- Ajax新玩法fetch API
- ajax的替代方案,Fetch
- fetch与传统的ajax
- Nutch流程之Fetch
- hibernate之fetch
- git命令之fetch
- fetch实践 替代jquery封装的ajax
- 传统的ajax将被fetch替代
- 传统 Ajax 已死,Fetch 永生
- “传说”未来代替Ajax的Fetch入门
- 使用 XHR、jQuery 和 Fetch 实现 AJAX
- fetch下一代ajax请求数据的封装
- 传统 Ajax 已死,Fetch 永生
- 格子取数2
- 关于烧写norflash愚蠢的错误
- mybatis常用配置
- 在树莓派上实现face++人脸识别
- 使用pyinstaller打包pyqt5出错问题解决
- AJax之Fetch
- 标签
- [00806]创建可管理的属性
- UOJ #278(【UTR #2】题目排列顺序-找规律)
- 【九度】 素数筛选1
- Android全屏的两种方法
- 关于下四楼文档中提升权限的想法
- android 选择手机图库,实现修改个人资料,头像上传功能,可直接使用
- C# XML序列化和反序列化