HTTP networking plugin that brings the whatwg fetch spec standard to Cordova
- Consistent with the
window.fetch
API based on the whatwg fetch spec - Supports iOS and Android
- Allows cross origin requests and ignores content security policy
- Allows all type of headers (including access to
Set-Cookie
andUser-Agent
)
MIT
The plugin conforms to the Cordova plugin specification, it can be installed using the Cordova / Phonegap command line interface.
cordova plugin add https://github.com/8bhsolutions/cordova-plugin-fetch.git
The cordovaFetch function supports any HTTP method. We'll focus on GET and POST example requests.
cordovaFetch('/users.html')
.then(function(response) {
return response.text()
}).then(function(body) {
document.body.innerHTML = body
})
cordovaFetch('/users.json')
.then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json', json)
}).catch(function(ex) {
console.log('parsing failed', ex)
})
cordovaFetch('/users.json', {
method : 'GET',
headers: {
'User-Agent': 'CordovaFetch 1.0.0'
},
})
cordovaFetch('/users.json')
.then(function(response) {
console.log(res.headers['Set-Cookie']);
})
var form = document.querySelector('form')
cordovaFetch('/users', {
method: 'POST',
body: new FormData(form)
})
cordovaFetch('/users.json', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'Hubot',
login: 'hubot',
})
})