Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between getJSON and $.ajax

Tags:

jquery

ajax

Right from the start i want to mention that i know THIS question has the same title as mine but that user asked a question regarding a problem he encountered.

My question about the differences is more subjective. I'm learning to use Jquery and Ajax and i came across both methods. Now to me they both seem to do the same. (get raw JSON data from a URL specified) but i'm sure there is a bigger difference.

I also noticed that people tend to use $.ajax more then getJSON, is there a reason for that aswell?

Any help is appreciated!

like image 593
Marco Geertsma Avatar asked Jan 15 '16 08:01

Marco Geertsma


People also ask

Is getJSON an AJAX call?

The getJSON() method is used to get JSON data using an AJAX HTTP GET request.

What is the difference between Get () and getJSON ()?

If you use getJSON, you still have to declare format=json in the url... And you can do the same in $. get() , and iterate through the JSON-object.

What is difference between AJAX and cURL?

cURL is a server-side process. This means that it will be called before the page is rendered and has nothing to do with the client's capabilities. AJAX, however, is a client-side call.

What is the difference between Jason and AJAX?

AJAX is utilizing for planning the internet page appropriately, particularly where the page needs a few server-side information without reviving the same. JSON isn't utilizing for only planning the net page. In fact, JSON some of the time not at all utilized for the net application.

What is the difference between Ajax and JSON?

After getting those proper data, AJAX normally transfers the same to the DOM element. And the DOM element is responsible for designing the possible web pages. JSON is a simple text to standardize defined format that holds a group of arrays in the javascript object.

What is the use of getjson in jQuery?

.getJson is simply a wrapper around .ajax but it provides a simpler method signature as some of the settings are defaulted e.g dataType to json, type to get etc N.B .load, .get and .post are also simple wrappers around the .ajax method. Show activity on this post. Further reading: 3 mistakes to avoid when using jQuery with ASP.NET

What is the difference between Ajax and XMLHttpRequest?

XMLHttpRequest is the raw browser object that jQuery wraps into a more usable and simplified form and cross browser consistent functionality. It’s basically a request to the browser to send a request and receive data. AJAX is an approach to make an HTTP request asynchronously without blocking the UI thread.

What are the advantages of using Ajax?

AJAX makes asynchronous calls to a web server, this means client browsers avoid waiting for all the data to arrive before starting of rendering. Form validation can be done successfully through it. Bandwidth utilization – It saves memory when the data is fetched from the same page. Ajax is dependent on Javascript.


1 Answers

$.getJSON()

From http://api.jquery.com/jquery.getjson/

This is a shorthand Ajax function, which is equivalent to:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

$.ajax()

From http://api.jquery.com/jquery.ajax/

dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

"json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests are converted to "jsonp" unless the request includes jsonp: false in its request options. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

That says that if you set dataType to JSON and if no JSON is returned, a parse error is thrown.

So judging from the docs, $.getJSON() is equal to $.ajax() with dataType set to "json", which means that if something different than JSON is returned, you end up with a parse error.

So you were mostly right about the two being pretty much the same :). $.getJSON() is just shorthand for the more extensive $.ajax().

like image 59
minitauros Avatar answered Oct 19 '22 15:10

minitauros