Fetch 是 window.fEtch 的 JavaScript polyfill。
全局 fetch 函数是 web 请求和处理响应的简单方式,不使用 XMLHTTPRequest。这个 polyfill 编写的接近标准的 Fetch 规范(https://fetch.spec.whatwg.org)。
fetch 函数支持所有的 HTTP 方式:
HTML:
fetch('/users.html')
.then(Function(response) {
return response.text()
}).then(function(body) {
document.body.innerHTML = body
})
JSON:
fetch('/users.json')
.then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json', json)
}).catch(function(ex) {
console.log('parsing failed', ex)
})
发布于 2016-01-21 13:50:43 | 1300 次阅读