“Fetch API” Kode Jawaban

Fetch API JavaScript

fetch('http://example.com/songs')
	.then(response => response.json())
	.then(data => console.log(data))
	.catch(err => console.error(err));
Bewildered Booby

Fetch JS

const data = { username: 'example' };

fetch('https://example.com/profile', {
  method: 'POST', // or 'PUT'
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
  console.log('Success:', data);
})
.catch((error) => {
  console.error('Error:', error);
});
Lively Ladybird

Fetch API

// Error handling while fetching API

const url = "http://dummy.restapiexample.com/api/v1/employee/40";
fetch(url) //404 error
     .then( res => {
          if (res.ok) {
                return res.json( );
          } else {
                return Promise.reject(res.status); 
           }
      })
      .then(res => console.log(res))
      .catch(err => console.log('Error with message: ${err}') ); 
Dhrey112

Fetch API

// Making get requests

const url = "http://dummy.restapiexample.com/api/v1/employees"; 
fetchurl()
     .then(res => {
            console.log(res);
})
      .catch(err => {
             console.log('Error: ${err}' ); 
});
Dhrey112

Fetch API


  const RcaApi = async () => {
    const url = await fetch(
      "http://quencies.alshumaal.com/api/RCAs/getallRcas.php"
    );
    const data = await url.json();
    setFetchData(data.getallRCAs);
  };
  useEffect(() => {
    RcaApi();
  }, [setFetchData]);
Blue Baboon

Fetch API

fetch('http://example.com/movies.json')
  .then(response => response.json())
  .then(data => console.log(data));

Jawaban yang mirip dengan “Fetch API”

Pertanyaan yang mirip dengan “Fetch API”

Lebih banyak jawaban terkait untuk “Fetch API” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya