Created
November 13, 2023 23:45
-
-
Save shah0150/b0f473a7231583734e845014caf61580 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
| let apiUrl = 'https://jsonplaceholder.typicode.com/todos'; | |
| // Data to be uploaded | |
| let postData = { | |
| title: 'Read materials for JS', | |
| completed: false | |
| } | |
| // Options or headers | |
| let options = { | |
| method: 'POST', //using POST to upload my data | |
| headers: { | |
| 'Content-Type': 'application/json' | |
| }, | |
| body: JSON.stringify(postData) | |
| }; | |
| // Make the fetch request | |
| fetch(apiUrl, options) | |
| .then(response => { | |
| if (!response.ok){ | |
| throw new Error('HTTP Error. Status: ${response.status}'); | |
| } | |
| return response.json() // parse the response as JSON | |
| }) | |
| .then(data => { | |
| console.log("Data successfully uploaded: ", data); | |
| }).catch(error => { | |
| console.log("Error uploading data: ", error); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment