react社区起来后,不知足如今的AJAX方案,搞了一个fetch。fetch只有在一些很是新的浏览器才支持,而github上的fetch却最多兼容到IE8,而且麻烦得要死,须要安装一大堆polyfill才能运行起来。javascript
因而我搞了一个兼容IE6的polyfill。用法与原生的如出一辙。html
npm install fetch-polyfill2
固然想要在IE6运行起来,还须要Promise与JSON3。java
推荐使用性能最好的bluebird与JSON3react
$ npm install bluebird -- save $ npm install json3 -- save
它内部是使用4种通讯手段与后端交换数据git
fetch('/users.html') .then(function(response) { return response.text() }).then(function(body) { document.body.innerHTML = body })
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) })
fetch('/users.json').then(function(response) { console.log(response.headers.get('Content-Type')) console.log(response.headers.get('Date')) console.log(response.status) console.log(response.statusText) })
var form = document.querySelector('form') fetch('/users', { method: 'POST', body: new FormData(form) })
fetch('/users', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'Hubot', login: 'hubot', }) })
var input = document.querySelector('input[type="file"]') var data = new FormData() data.append('file', input.files[0]) data.append('user', 'hubot') fetch('/avatars', { method: 'POST', body: data })
fetch('/users', { //jsonp!!! credentials: 'include', }).then(function(response){ return response.json() }).then(function(){ })
欢迎star与pull request https://github.com/RubyLouvre...github