Categories: Json

JSON with Ajax

Share

Ajax is Asynchronous JavaScript and XML for for asynchronous web applications. Using ajax data from a server asynchronously retrieved and display without reloading the page. So JSON with Ajax is no exception.

JSON stands for JavaScript Object Notation. So, in simple terms JSON is a way of formatting the data. For, e.g., transmitting it over a network.

The key advantage of using JSON is quality programming. JSON is less expressed and covered, resulting in fewer bytes and a faster parsing process which allows us to process more messages sent as JSON than as XML.

In our previous article, we have already discussed the Syntax of JSON.

Now let’s see how jQuery can help us to load JSON encoded data from a remote source.

Here is a simple example JSON with Ajax:

<!DOCTYPE html>
<html>
<body>
<h1>Student List</h1>
<div id="studentDiv"></div>

<script>
$.ajax({
  url: 'ajax.php',
  type: 'GET',
  data: 'class=v',
  success: function(response) {
    var arr = JSON.parse(response);
    var i;
    var str = "<table>";
    for(i = 0; i < arr.length; i++) {
        str += "<tr><td>" +
        arr[i].Name +
        "</td><td>" +
        arr[i].Class +
        "</td><td>" +
        arr[i].Subject +
        "</td></tr>";
    }
    str += "</table>";
    $("#studentDiv").html(str);
  },
  error: function(e) {
    console.log(e.message);
  }
});
</script>

</body>
</html>

JSON is a de-facto standard format for exchanging any text data. jQuery’s $.getJSON() method gives a nice little helper for dealing with almost any scenario that involves a request for JSON formatted data.

View Comments

Published by
Jacob Frazier

Recent Posts

Unveiling the Truth: Is the Spread of Sinus Infections a Myth or Reality?

Sinus infections, impacting approximately 31 million Americans each year, represent a significant health concern stemming…

16 hours ago

ARTIFICIAL INTELLIGENCE: Advantages And Disadvantages? Everything You Need to Know

Pros And Cons Of AI: Artificial Intelligence directly translates to conceptualizing and building machines that…

1 day ago

Four common mistakes when picking an internet provider

We all live on the internet; we use it for everything. Thus, when it comes…

1 day ago

What Is Commonly Misdiagnosed as Pink Eye: Understanding Eye Conditions and Their Symptoms

Introduction: Pink eye, or conjunctivitis, is a common eye condition characterized by redness and inflammation…

2 days ago

Why Flexible Financing is the Future of Small Business

Small businesses are the backbone of the economy. Still, they often face daunting hurdles when…

2 days ago

Warm Comfort: Choosing the Best Hot Water Bottle for Cozy Nights

Introduction: As the chill of winter settles in or a bout of cold weather strikes,…

3 days ago