Code With Mark
Home
About
Resources
Contact

How To Easily Use Ajax/jQuery.getJSON

Most of the web services are switching from XML to JSON data for their results. Learn how you can easily use json data in your web applications.  

What is JSON?

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

You will learn a few ways to load JSON data in your web applications.

Javascript JSON Function

You can use plain javascript request to get json data from a URL.

Below is a simple javascript json function:

var getJSON = function(url, callback) 
{
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'json';
    xhr.onload = function() 
    {
      var status = xhr.status;
      if (status === 200) 
      {
        callback(null, xhr.response);
      } else {
        callback(status, xhr.response);
      }
    };
    xhr.send();
};

To use this json function, just call it like this:

getJSON( 'https://randomuser.me/api/' ,function(err, data)
{
	console.log(data);
})
	

JSON jQuery Syntax

You can also use jquery getjson function like this:

$.getJSON('https://apimk.com/ip.json' , function(data) 
{
	console.log(data);

});

$.Ajax Function

You can also user jquery ajax function to get your url data like this:

$.ajax({
	url: 'https://ghibliapi.herokuapp.com/films',
	type: 'GET',
	dataType: 'json',
})
.done(function(data) {
	console.log("success", data);
})
.fail(function(data) 
{
	console.log("error");
});

These json functions will definitely come in handy in your web applications to get json data from a url.

For Web Developers

Working Hard But Still Not Getting Ahead?

You finish a project, get paid, and then it's back to finding the next client.

Month after month, the cycle repeats.

That's why many web developers never build real financial freedom—even though they're highly skilled.

The developers creating long-term wealth are using those same skills to build SaaS products, plugins, and digital tools that generate recurring income.

What if your next project could pay you more than once?

Learn How To Build Monthly Income →
Convert numbers to words (number spelling) in JavaScriptConvert numbers to words (number spelling) in JavaScript←Previous
JavaScript Storage Like MySQLJavaScript Storage Like MySQLNext→

Related Posts

  • How Google Developers Think (And Why You Should Too)
  • Add Google Sign-In in 2 Minutes
  • Form Validation in 1 Line

Top Posts Viewed

Easily Edit HTML Table Rows Or Cells With jQuery
145 views
PHP Simple Database Class
135 views
How To Create A Secure Login System With PHP And MySQL
134 views

Categories

Courses
Excel
Google Script
Javascript
jQuery
Microsoft Access
MongoDB
Node JS
PHP
Quick Tip
Uncategorized
Wordpress