# fetch-polyfill **Repository Path**: smanjusa/fetch-polyfill ## Basic Information - **Project Name**: fetch-polyfill - **Description**: fetch polyfill which supports all mainstream browsers, even IE6, IE7, IE8..... - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-12-08 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # fetch-polyfill fetch polyfill which supports all mainstream browsers, even IE6, IE7, IE8..... $ npm install fetch-polyfill2 --save $ npm install promise -- save ### HTML ```javascript fetch('/users.html') .then(function(response) { return response.text() }).then(function(body) { document.body.innerHTML = body }) ``` ### JSON ```javascript 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) }) ``` ### Response metadata ```javascript 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) }) ``` ### Post form ```javascript var form = document.querySelector('form') fetch('/users', { method: 'POST', body: new FormData(form) }) ``` ### Post JSON ```javascript fetch('/users', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'Hubot', login: 'hubot', }) }) ``` ### File upload ```javascript 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 }) ``` ###IE6-7 cors ```javascript fetch('/users', { //jsonp!!! credentials: 'include', }).then(function(response){ return response.json() }).then(function(){ }) ```