avalon官方AJAX庫fetch polyfill 發佈0.02版,修復衆多BUG。javascript
https://github.com/RubyLouvre...java
用法示例node
avalon.fetch('/getAjax?aaa=1&&bb=2',{ method: 'GET' }).then(function(a){ return a.json() }).then(function(a){ console.log(a) }) avalon.fetch('/postAJax', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ firstParam: 'yourValue', secondParam: 'yourOtherValue', }) }).then(function(a){ return a.json() }).then(function(a){ console.log(a) }) function toQueryString(obj) { return obj ? Object.keys(obj).sort().map(function (key) { var val = obj[key]; if (Array.isArray(val)) { return val.sort().map(function (val2) { return encodeURIComponent(key) + '=' + encodeURIComponent(val2); }).join('&'); } return encodeURIComponent(key) + '=' + encodeURIComponent(val); }).join('&') : ''; } avalon.fetch('/postAJax', { method: 'post', headers: { 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'x-www-form-urlencoded' }, body: toQueryString({ 'name': 'aaa', 'pw':'bbb' }) }).then(function(a){ return a.json() }).then(function(a){ console.log(a) })
安裝fetch 庫git
$ npm install fetch-polyfill2 --save $ npm install bluebird -- save $ npm install json3 -- save
運行例子github
跑到koa目錄下npm
npm install node index
瀏覽器下打開localhost: 4000json