JavaScript JSON Server

Back to JS page

Table of Contents

The fetch() Method

The fetch() method is used to send and receive data from a server.

It returns a Promise that resolves to a Response object.

The response.json() method parses the response body as JSON.

Loading JSON from a Server

Use fetch() to load a JSON file from a server, then use response.json() to parse it:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON Server</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Loading JSON from a Server</h4>
<p id="demo"></p>

<script>
fetch('https://jsonplaceholder.typicode.com/users/1')
  .then(response => response.json())
  .then(data => {
    document.getElementById("demo").innerHTML =
      "Name: " + data.name + "<br>" +
      "Email: " + data.email + "<br>" +
      "City: " + data.address.city;
  })
  .catch(error => {
    document.getElementById("demo").innerHTML = "Error: " + error.message;
  });
</script>

</body>
</html>

Example 1

Result View Example

Loading a JSON Array

You can also load a JSON array from a server:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON Server</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Loading a JSON Array</h4>
<p id="demo"></p>

<script>
fetch('https://jsonplaceholder.typicode.com/users')
  .then(response => response.json())
  .then(data => {
    let text = "<ul>";
    for (let user of data) {
      text += "<li>" + user.name + " - " + user.email + "</li>";
    }
    text += "</ul>";
    document.getElementById("demo").innerHTML = text;
  })
  .catch(error => {
    document.getElementById("demo").innerHTML = "Error: " + error.message;
  });
</script>

</body>
</html>

Example 2

Result View Example

Handling Errors

Always handle errors when loading JSON from a server. Check the response status:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON Server</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Handling Errors</h4>
<p id="demo"></p>

<script>
fetch('https://jsonplaceholder.typicode.com/users/nonexistent')
  .then(response => {
    if (!response.ok) {
      throw new Error("HTTP error! Status: " + response.status);
    }
    return response.json();
  })
  .then(data => {
    document.getElementById("demo").innerHTML = "Name: " + data.name;
  })
  .catch(error => {
    document.getElementById("demo").innerHTML = "Error: " + error.message;
  });
</script>

</body>
</html>

Example 3

Result View Example

Sending JSON

You can use fetch() with the POST method to send JSON data to a server:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript JSON Server</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Sending JSON</h4>
<p id="demo"></p>

<script>
const userData = {
  name: "John Doe",
  email: "john@example.com",
  username: "johndoe"
};

fetch('https://jsonplaceholder.typicode.com/users', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(userData)
})
  .then(response => response.json())
  .then(data => {
    document.getElementById("demo").innerHTML =
      "User created!<br>" +
      "ID: " + data.id + "<br>" +
      "Name: " + data.name + "<br>" +
      "Email: " + data.email;
  })
  .catch(error => {
    document.getElementById("demo").innerHTML = "Error: " + error.message;
  });
</script>

</body>
</html>

Example 4

Result View Example

Document

Document in project

You can Download PDF file.

Reference