Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript fetch json

fetch('./yourjson.json')
  .then((response) => response.json())
  .then((data) => {
  	console.log(data);
  })
Comment

js fetch send json

fetch('https://jsonplaceholder.typicode.com/posts', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
   body: JSON.stringify({
     // your expected POST request payload goes here
      title: "My post title",
      body: "My post content."
      })
})
  .then(res => res.json())
  .then(data => {
   // enter you logic when the fetch is successful
    console.log(data)
  })
  .catch(error => {
  // enter your logic for when there is an error (ex. error toast)
   console.log(error)
  })
Comment

convert fetch data to json

fetch("localhost:3000/api/home") // first step
  .then(response => response.json()) // second step
  .then(data => {
    console.log(data)
  })
  .catch(error => console.error(error))
Comment

js fetch json

var myRequest = new Request('products.json');//GET
var myRequest = new Request('products.json', {method: "post"});//POST

fetch(myRequest)
  .then(response => response.json())
  .then(data => {
    console.log(data);
  })
  .catch(console.error);
Comment

get jsonp with fetch

fetchJsonp('/users.jsonp')
  .then(function(response) {
    return response.json()
  }).then(function(json) {
    console.log('parsed json', json)
  }).catch(function(ex) {
    console.log('parsing failed', ex)
  })
Comment

Use the fetch API to load the JSON

fetch("animals.json")
    .then(response => response.json())
    .then(data => {
        // Work with your data here
    });
Comment

PREVIOUS NEXT
Code Example
Javascript :: Set Default Parameter Value 
Javascript :: js object destructuring 
Javascript :: JavaScript and HTML DOM Reference 
Javascript :: reverse integer in for javascript 
Javascript :: expo app.json 
Javascript :: for loop in javacript 
Javascript :: belongstomany sequelize 
Javascript :: Javascript "For..in Loop" Syntax 
Javascript :: how to use object destructuring 
Javascript :: es6 class example 
Javascript :: js await 
Javascript :: firebase realtime database javascript 
Javascript :: how to hide a button in react 
Javascript :: pm2 change log timestamp 
Javascript :: conver all array to object 
Javascript :: Return with an "IF" Statement 
Javascript :: terjemahan 
Javascript :: javascript get the last array element 
Javascript :: clone an object javascript 
Javascript :: console.log is not a function 
Javascript :: slice() javascript 
Javascript :: javascript expression interpolation 
Javascript :: nginx location regex * 
Javascript :: pretty print javascript 
Javascript :: how to change background color in css and or js react 
Javascript :: what does the useReducer do in react 
Javascript :: how to call function with only selected arguments in javascript 
Javascript :: filter based on input typing react 
Javascript :: functions like once 
Javascript :: how to remove document.getElementById("myDropdown").classList.toggle("show"); 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =