Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

fetch data from api url

fetch('https://jsonplaceholder.typicode.com/posts').then(function (response) {
	// The API call was successful!
	return response.json();
}).then(function (data) {
	// This is the JSON from our response
	console.log(data);
}).catch(function (err) {
	// There was an error
	console.warn('Something went wrong.', err);
});
Comment

js how to get data fetch

// way 1
const getData = async (url) => {
  const response = await fetch(url)
  const json = await response.json()

  return json
}

const url = `https://jsonplaceholder.typicode.com/posts?userId=3`

try {
  const data = await getData(url)
  console.log(data)
} catch (error) {
  console.log(error.message)
}

/* ------------ Way 2 --------------- */
const getData = (url) =>
  new Promise((resolve, reject) => {
    fetch(url)
      .then(response => response.json())
      .then(json => resolve(json))
      .catch(error => reject(error))
  })

const url = `https://jsonplaceholder.typicode.com/todos?completed=true&userId=2`

getData(url)
  .then(data => console.log(data))
  .catch(error => console.log(error.message))
Comment

js undici fetch data

    import {fetch} from 'undici';

    async function fetchJson() {
        const res = await fetch('https://example.com')
        const json = await res.json()
        console.log(json);
    
Comment

fetch to get data from server

fetch(url).then(function(response) {
  response.text().then(function(dataFromServer) {
    //do stuff
  });
});
Comment

fetch api data javascript

.catch(function() {

});
Comment

javascript how to fetch data

.catch(function() {

});
Comment

PREVIOUS NEXT
Code Example
Javascript :: react state hooks 
Javascript :: get all links from html javascript 
Javascript :: click outside javascript 
Javascript :: jquery select specific radio button by value 
Javascript :: how to sort array by dates 
Javascript :: Handle an input with React hooks 
Javascript :: node command line input 
Javascript :: lodash remove undefined values from object 
Javascript :: jquery get all text inputs 
Javascript :: do while loop 
Javascript :: Generate Random Whole Numbers within a Range 
Javascript :: get datetime yesterday angular 
Javascript :: javascript trim 
Javascript :: strapi production build 
Javascript :: javascript password regular expression 
Javascript :: refresh page and run function after javascript 
Javascript :: how to get datetime javascript now 
Javascript :: javascript enumerate with index 
Javascript :: set url parameters javascript 
Javascript :: how to compare strings in javascript ignoring case sensitive 
Javascript :: react router dom 
Javascript :: select all checkbox jquery 
Javascript :: Scrollbar inside Dropdown of antD component React 
Javascript :: jquery animation 
Javascript :: sh: 1: react-scripts: not found 
Javascript :: javascript include js file 
Javascript :: Calling MVC controller from Javascript ajax 
Javascript :: set multiple attributes css javascript 
Javascript :: console.time 
Javascript :: npm http angular 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =