jQuery ajax getJson example

jQuery ajax getJson method is used to load json data from server using http get method. Lets go through syntax for getJson method.
Url:It is url for get request on server
data:Data which you want to send to server with request.
Success :Callback function which get executed when request succeeds.
Data which you send with get request will be in the form of query string.

jQuery getJson example:

Lets first create the json file named country.json which we are going to read from server. country.json

Lets create html file named jQueryGetJson.html.

Lets deploy html file on server and send getJson request.

When you click on Get country data , you will get below output

Explanation:

  • $(document).ready(function() : This function will get called when fully DOM is loaded.
  • $(“button”).click(function(event) : This function will get called when “get country data” button is clicked.
  •  $.getJSON(“country.json”, function(country) : Here we are calling getJson method and URL is “country.json”.  When request is successfully completed, callback function will be get called.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *