File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ // Fetching the data.
2+
3+ const url = `https://jsonplaceholder.typicode.com/todos`
4+ const fetchData = async ( ) => {
5+ try {
6+ const response = await fetch ( url ) ;
7+ if ( ! response . ok ) { console . log ( "Error with the source" ) }
8+ const data = await response . json ( ) ;
9+
10+ console . log ( data [ 0 ] . title ) ;
11+ } catch ( error ) {
12+ console . log ( error ) ;
13+ }
14+ }
15+
16+ fetchData ( )
17+
18+
19+ // post data using the fetch method
20+
21+ const postData = async ( ) => {
22+ try {
23+ const response = await fetch ( "https://jsonplaceholder.typicode.com/todos" ,
24+
25+ {
26+ method :'POST' ,
27+ body :JSON . stringify ( {
28+ title :'Welcome'
29+ } ) ,
30+ headers :{
31+ 'Content-Type' :'application/json'
32+ }
33+ } )
34+ const data = await response . json ( )
35+ console . log ( data )
36+ } catch ( error ) {
37+ console . log ( error )
38+ }
39+ }
40+
41+ postData ( ) ;
You can’t perform that action at this time.
0 commit comments