Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

return value from fetch javascript

  function getvals(){
    return fetch('https://jsonplaceholder.typicode.com/posts',
    {
    	method: "GET",
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
    })
    .then((response) => response.json())
    .then((responseData) => {
      console.log(responseData);
      return responseData;
    })
    .catch(error => console.warn(error));
  }
  
  getvals().then(response => console.log(response));
Comment

return fetch javascript

async function yourFunction() { //Most compact way to return a fetch
    const response = await fetch('some-url', {}); 
    const json = await response.json();
    return json; //do here wathever with your json if you want to return
}				//a specific part of it.

yourFunction().then(resp => {
    console.log(resp); //Here you get the function response and print it
});
Comment

javascript fetch request GET

// Update fields in form based on API GET request
function update_form_fields(term, field){ 
  fetch("/api/profiles/?format=json")
    .then((response)=>{
    return response.json();
  }).then((data) => {
    let profile = data.find(el => el[field] == term);      
    document.getElementById("name-input").value = profile.name;
    document.getElementById("email-input").value = profile.email;
  });}
Comment

display fetch response js

// This will give you a response status code
console.log(response.status)
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery left arrow key press 
Javascript :: generate random string in javascript 
Javascript :: round to nearest hundredth javascript 
Javascript :: react app using npm 
Javascript :: check if an id exists javascript 
Javascript :: min max and average finder in js array 
Javascript :: Angular detecting escape key press 
Javascript :: js parse url decode 
Javascript :: jqeury cdn 
Javascript :: jquery selected option text 
Javascript :: for loop display array 
Javascript :: javascript remove element 
Javascript :: mui textfield font color 
Javascript :: pass only numbers in input react js 
Javascript :: ajax request 
Javascript :: refresh page after success ajax 
Javascript :: fuse.js cdn 
Javascript :: how to draw horizontal line in canvas 
Javascript :: check data in formData 
Javascript :: nodejs readfile 
Javascript :: javascript float 2 decimal 
Javascript :: javascript array foreach example 
Javascript :: switch browser to fullscreen 
Javascript :: useeffect not working with react-dom-router 
Javascript :: on modal close jquery 
Javascript :: angular input date binding on variable update 
Javascript :: js check if infinity 
Javascript :: remove multiple space to single javascript 
Javascript :: widget is not working in arcgis map javascript 
Javascript :: stun server 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =