Created
July 28, 2017 20:05
-
-
Save prof3ssorSt3v3/dbc5efa03371068e55367b1278e5ba22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//fetch using a Request and a Headers objects | |
//using jsonplaceholder for the data | |
const uri = 'http://jsonplaceholder.typicode.com/users'; | |
//new Request(uri) | |
//new Request(uri, options) | |
//options - method, headers, body, mode | |
//methods: GET, POST, PUT, DELETE, OPTIONS | |
//new Headers() | |
// headers.append(name, value) | |
// Content-Type, Content-Length, Accept, Accept-Language, | |
// X-Requested-With, User-Agent | |
let h = new Headers(); | |
h.append('Accept', 'application/json'); | |
let req = new Request(uri, { | |
method: 'POST', | |
headers: h, | |
mode: 'cors' | |
}); | |
fetch(req) | |
.then( (response)=>{ | |
if(response.ok){ | |
return response.json(); | |
}else{ | |
throw new Error('BAD HTTP stuff'); | |
} | |
}) | |
.then( (jsonData) =>{ | |
console.log(jsonData); | |
}) | |
.catch( (err) =>{ | |
console.log('ERROR:', err.message); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment