Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created August 2, 2017 01:15
Show Gist options
  • Save prof3ssorSt3v3/9b23755a0f40bca9143fe27e47cfd905 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/9b23755a0f40bca9143fe27e47cfd905 to your computer and use it in GitHub Desktop.
//Sending Data to the Server using Fetch()
//using jsonplaceholder for the data
//GET - queryStrings
// http://jsonplaceholder.typicode.com/posts?userId=1&postId=65
// http://jsonplaceholder.typicode.com/todos?userId=2
//POST
// http://jsonplaceholder.typicode.com/posts
const root = 'http://jsonplaceholder.typicode.com/';
let uri = root + 'posts';
let formdata = new FormData();
formdata.append("userId", 3);
formdata.append('title','This is my title');
formdata.append('body','This is the body text of the post');
let options = {
method: 'POST',
mode: 'cors',
body: formdata
}
let req = new Request(uri, options);
fetch(req)
.then((response)=>{
if(response.ok){
return response.json();
}else{
throw new Error('Bad HTTP!')
}
})
.then( (j) =>{
console.log(j);
})
.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