Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between $.getJSON() and $.ajax() in jQuery

I am calling an ASP.NET MVC action

public JsonResult GetPatient(string patientID) { ... 

from JavaScript using jQuery. The following call works

$.getJSON( '/Services/GetPatient', { patientID: "1" }, function(jsonData) {   alert(jsonData); }); 

whereas this one does not.

$.ajax({   type: 'POST',   url: '/Services/GetPatient',   data: { patientID: "1" },   contentType: 'application/json; charset=utf-8',   dataType: 'json',   success: function(jsonData) {     alert(jsonData);   },   error: function() {     alert('Error loading PatientID=' + id);   } }); 

Both reach the action method, but the patientID value is null w/ the $.ajax call. I'd like to use the $.ajax call for some of the advanced callbacks.

Any thoughts appreciated.

like image 278
ChrisP Avatar asked Jul 02 '09 18:07

ChrisP


People also ask

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 jQuery getJSON?

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

What is Ajax used for?

What's AJAX? AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.

What is an Ajax call?

An Ajax call is an asynchronous request initiated by the browser that does not directly result in a page transition. A servlet request is a Java-specifc term (servlets are a Java specification) for servicing an HTTP request that could get a simple GET or POST (etc) or an Ajax request.


1 Answers

Content-type

You don't need to specify that content-type on calls to MVC controller actions. The special "application/json; charset=utf-8" content-type is only necessary when calling ASP.NET AJAX "ScriptServices" and page methods. jQuery's default contentType of "application/x-www-form-urlencoded" is appropriate for requesting an MVC controller action.

More about that content-type here: JSON Hijacking and How ASP.NET AJAX 1.0 Avoids these Attacks

Data

The data is correct as you have it. By passing jQuery a JSON object, as you have, it will be serialized as patientID=1 in the POST data. This standard form is how MVC expects the parameters.

You only have to enclose the parameters in quotes like "{ 'patientID' : 1 }" when you're using ASP.NET AJAX services. They expect a single string representing a JSON object to be parsed out, rather than the individual variables in the POST data.

JSON

It's not a problem in this specific case, but it's a good idea to get in the habit of quoting any string keys or values in your JSON object. If you inadvertently use a JavaScript reserved keyword as a key or value in the object, without quoting it, you'll run into a confusing-to-debug problem.

Conversely, you don't have to quote numeric or boolean values. It's always safe to use them directly in the object.

So, assuming you do want to POST instead of GET, your $.ajax() call might look like this:

$.ajax({   type: 'POST',   url: '/Services/GetPatient',   data: { 'patientID' : 1 },   dataType: 'json',   success: function(jsonData) {     alert(jsonData);   },   error: function() {     alert('Error loading PatientID=' + id);   } }); 
like image 65
Dave Ward Avatar answered Sep 21 '22 01:09

Dave Ward