Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does jQuery have built in JSON support?

Tags:

json

jquery

Does jQuery have built in JSON support or must I use a plugin like jquery.json-1.3.min.js ?

like image 663
JasonDavis Avatar asked Aug 11 '09 14:08

JasonDavis


People also ask

Does jQuery use JSON?

Projects In JavaScript & JQueryTo load JSON data using jQuery, use the getJSON() and ajax() method. The jQuery. getJSON( ) method loads JSON data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server.

Is jQuery the same as JSON?

Json: JSON is a text format that is completely language independent. JQuery:It is a fast and minified JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.

Is Ajax built in jQuery?

The jQuery library has a full suite of Ajax capabilities. The functions and methods therein allow us to load data from the server without a browser page refresh.

What is JSON parse in jQuery?

parseJSON( json )Returns: String or Number or Object or Array or Booleanversion deprecated: 3.0. Description: Takes a well-formed JSON string and returns the resulting JavaScript value.


3 Answers

You can also use $.ajax and set the dataType option to "json":

 $.ajax({
      url: "script.php",
      global: false,
      type: "POST",
      data: ({id : this.getAttribute('id')}),
      dataType: "json",
      success: function(json){
         alert(json.foo);
      }
   }
);

Also, $.get and $.post have an optional fourth parameter that allows you to set the data type of the response, e.g.:

$.postJSON = function(url, data, callback) {
    $.post(url, data, callback, "json");
};

$.getJSON = function(url, data, callback) {
    $.get(url, data, callback, "json");
};
like image 124
karim79 Avatar answered Oct 15 '22 22:10

karim79


Yes, absolutely it does. You can do something like:

$.getJSON('/foo/bar/json-returning-script.php', function(data) {
    // data is the JSON object returned from the script.
});
like image 32
VoteyDisciple Avatar answered Oct 15 '22 22:10

VoteyDisciple


jQuery's JSON support is simplistic, throwing caution to the wind. I've used $.ajax and then parse the response text with the json.org javascript library. It lexically parses to avoid using eval() and possibly executing arbitrary code.

like image 22
spoulson Avatar answered Oct 15 '22 21:10

spoulson