Last active
January 17, 2022 08:16
-
-
Save costa86/80419962e29e5adbdefc006494f3627b to your computer and use it in GitHub Desktop.
Post request with pure JS - no jQuery/ajax
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
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