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));
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
});
// 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;
});}
// This will give you a response status code
console.log(response.status)