Skip to content

Instantly share code, notes, and snippets.

@costa86
Last active January 17, 2022 08:16
Show Gist options
  • Save costa86/80419962e29e5adbdefc006494f3627b to your computer and use it in GitHub Desktop.
Save costa86/80419962e29e5adbdefc006494f3627b to your computer and use it in GitHub Desktop.
Post request with pure JS - no jQuery/ajax
async function getData(){
let url = 'https://jsonplaceholder.typicode.com/posts/1';
let page = await fetch(url);
let json = await page.json();
console.log(json);
}
async function postData(){
let url = 'https://jsonplaceholder.typicode.com/posts';
let person = {
firstName:"jason",
lastName:"bourne"
};
let headers = {
'Content-type': 'application/json; charset=UTF-8'
};
let request = {
method:"POST",
body:JSON.stringify(person),
headers
};
let page = await fetch(url,request);
let json = await page.json();
console.log(json);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment