0% found this document useful (0 votes)
86 views

tjSON - JQuery API Documentation

The jQuery.getJSON() method makes a GET request to a specified URL and expects JSON data in return. It allows passing data to the server and a callback function to handle the response. The callback receives the parsed JSON response and text status of the request. The method returns a jqXHR object that implements the Promise interface and can be used with done(), fail(), and always() methods.

Uploaded by

Maxwell Smart
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

tjSON - JQuery API Documentation

The jQuery.getJSON() method makes a GET request to a specified URL and expects JSON data in return. It allows passing data to the server and a callback function to handle the response. The callback receives the parsed JSON response and text status of the request. The method returns a jqXHR object that implements the Promise interface and can be used with done(), fail(), and always() methods.

Uploaded by

Maxwell Smart
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.

getjson/

Plugins (https://plugins.jquery.com/) Contribute (https://contribute.jquery.org/) Events (https://js.foundation/events)

Support (https://jquery.org/support/) JS Foundation (https://js.foundation/)

Download (https://jquery.com/download/) Search 

API Documentation (https://api.jquery.com/)

Blog (https://blog.jquery.com/)

Plugins (https://plugins.jquery.com/)

Browser Support (https://jquery.com/browser-support/)

jQuery.getJSON()
Categories: Ajax (//api.jquery.com/category/ajax/) > Shorthand Methods (//api.jquery.com/
category/ajax/shorthand-methods/)

jQuery.getJSON( url [, data ] [, success ] )


Returns: jqXHR (http://api.jquery.com/Types/#jqXHR)

 jQuery.getJSON( url [, data version added: 1.0 (/category/version/1.0/)


] [, success ] )

Type: String (http://api.jquery.com/Types/#String)


A string containing the URL to which the request is sent.

Type: PlainObject (http://api.jquery.com/Types/#PlainObject) or


String (http://api.jquery.com/Types/#String)
A plain object or string that is sent to the server with the request.

Type: Function (http://api.jquery.com/Types/#Function)(


PlainObject (http://api.jquery.com/Types/#PlainObject) data, String
(http://api.jquery.com/Types/#String) textStatus, jqXHR (http://
1 of 14 9/10/23, 22:29
jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.getjson/

Ajax (// api.jquery.com/Types/#jqXHR) jqXHR )


A callback function that is executed if the request succeeds.
api.jquery.com/
category/ajax/)
This is a shorthand Ajax function, which is equivalent to:
Global Ajax Event
Handlers (//
api.jquery.com/ 1 $.ajax({
2 dataTpe: "jon",
category/ajax/ 3 url: url,
global-ajax-event- 4 data: data,
5 ucce: ucce
handlers/)
6 });
Helper Functions
(//api.jquery.com/ Data that is sent to the server is appended to the URL as a query string. If
category/ajax/ the value of the data parameter is a plain object, it is converted to a
helper-functions/) string and url-encoded before it is appended to the URL.
Low-Level
Most implementations will specify a success handler:
Interface (//
api.jquery.com/
1 $.getJON( "ajax/tet.jon", function( data ) {
category/ajax/low-
2 var item = [];
level-interface/) 3 $.each( data, function( ke, val ) {
4 item.puh( "<li id='" + ke + "'>" + val + "</li>"
Shorthand 5 });
Methods (// 6
7 $( "<ul/>", {
api.jquery.com/ 8 "cla": "m-new-lit",
category/ajax/ 9 html: item.join( "" )
shorthand- 10 }).appendTo( "od" );
11 });
methods/)

This example, of course, relies on the structure of the JSON file:


Attributes (//
api.jquery.com/
1 {
category/attributes/)
2 "one": "ingular enation",
3 "two": "ead little ee",
Callbacks Object (//
4 "three": "Little ird pitch  m doortep"
api.jquery.com/ 5 }
category/callbacks-
object/)
Using this structure, the example loops through the requested data,
Core (// builds an unordered list, and appends it to the body.
api.jquery.com/
The ucce callback is passed the returned data, which is typically a
category/core/)
JavaScript object or array as defined by the JSON structure and parsed
CSS (// using the $.pareJON() (/jQuer.pareJON/) method. It is also
api.jquery.com/ passed the text status of the response.
category/css/)
, the ucce callback function receives a "jqXHR"
Data (//
object (/jQuery.get/#jqxhr-object) (in , it received the
api.jquery.com/
XMLHttpRequet object). However, since JSONP and cross-domain GET
category/data/)
requests do not use XHR (XMLHTTPRequest), in those cases the jqXHR

2 of 14 9/10/23, 22:29
jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.getjson/

Deferred Object (// and texttatu parameters passed to the success callback are
api.jquery.com/ undefined.
category/deferred-
object/) As of jQuery 1.4, if the JSON file contains a syntax
error, the request will usually fail silently. Avoid frequent hand-
Deprecated (//
editing of JSON data for this reason. JSON is a data-interchange
api.jquery.com/
format with syntax rules that are stricter than those of
category/
JavaScript's object literal notation. For example, all strings
deprecated/)
represented in JSON, whether they are properties or values,
Deprecated 1.3 (// must be enclosed in double-quotes. For details on the JSON
api.jquery.com/ format, see https://json.org/ (https://json.org/).
category/
deprecated/
deprecated-1.3/) JSONP

Deprecated 1.7 (// If the URL includes the string "callback=?" (or similar, as defined by the
api.jquery.com/ server-side API), the request is treated as JSONP instead. See the
category/ discussion of the jonp data type in $.ajax() (/jQuer.ajax/) for
deprecated/ more details.
deprecated-1.7/)
The jqXHR Object
Deprecated 1.8 (//
api.jquery.com/ , all of jQuery's Ajax methods return a superset of the
category/ XMLHTTPRequet object. This jQuery XHR object, or "jqXHR," returned by
deprecated/ $.getJON() implements the Promise interface, giving it all the
deprecated-1.8/) properties, methods, and behavior of a Promise (see Deferred object (/
category/deferred-object/) for more information). The jqXHR.done() (for
Deprecated 1.9 (//
success), jqXHR.fail() (for error), and jqXHR.alwa() (for
api.jquery.com/
completion, whether success or error; added in jQuery 1.6) methods take
category/
a function argument that is called when the request terminates. For
deprecated/
information about the arguments this function receives, see the jqXHR
deprecated-1.9/)
Object (/jQuery.ajax/#jqXHR) section of the $.ajax() documentation.
Deprecated 1.10
(//api.jquery.com/ The Promise interface in jQuery 1.5 also allows jQuery's Ajax methods,
category/ including $.getJON() , to chain multiple .done() , .alwa() , and
.fail() callbacks on a single request, and even to assign these
deprecated/
deprecated-1.10- callbacks after the request may have completed. If the request is already
and-2.0/) complete, the callback is fired immediately.

Deprecated 3.0 (//


api.jquery.com/
category/
deprecated/
deprecated-3.0/)

Deprecated 3.2 (//


api.jquery.com/
category/

3 of 14 9/10/23, 22:29
jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.getjson/
deprecated/ 1 // Aign handler immediatel after making the requet,
2 // and rememer the jqxhr oject for thi requet
deprecated-3.2/)
3 var jqxhr = $.getJON( "example.jon", function()
4 conole.log( "ucce" );
Deprecated 3.3 (//
5 })
api.jquery.com/ 6 .done(function() {
category/ 7 conole.log( "econd ucce" );
8 })
deprecated/ 9 .fail(function() {
deprecated-3.3/) 10 conole.log( "error" );
11 })
Deprecated 3.4 (// 12 .alwa(function() {
api.jquery.com/ 13 conole.log( "complete" );
14 });
category/ 15
deprecated/ 16 // Perform other work here ...
deprecated-3.4/) 17
18 // et another completion function for the requet aove
Deprecated 3.5 (// 19 jqxhr.alwa(function() {
20 conole.log( "econd complete" );
api.jquery.com/ 21 });
category/
deprecated/
Deprecation Notice
deprecated-3.5/)
The jqXHR.ucce() , jqXHR.error() , and jqXHR.complete()
Dimensions (// callback methods are . You can use
api.jquery.com/ jqXHR.done() , jqXHR.fail() , and jqXHR.alwa() instead.

category/
Additional Notes:
dimensions/)
Due to browser security restrictions, most "Ajax" requests are
Effects (//
subject to the same origin policy (https://en.wikipedia.org/wiki/
api.jquery.com/
Same_origin_policy); the request can not successfully retrieve data
category/effects/) from a different domain, subdomain, port, or protocol.
Basics (//
Script and JSONP requests are not subject to the same origin
api.jquery.com/ policy restrictions.
category/effects/
basics/)
Examples:
Custom (//
Loads the four most recent pictures of Mount Rainier from the Flickr
api.jquery.com/
JSONP API.
category/effects/
custom-effects/)

Fading (//
api.jquery.com/
category/effects/
fading/)

Sliding (//
api.jquery.com/
category/effects/
sliding/)

4 of 14 9/10/23, 22:29
jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.getjson/

Events (// 1 <!doctpe html>


2 <html lang="en">
api.jquery.com/ 3 <head>
category/events/) 4 <meta charet="utf-8">
5 <title>jQuer.getJON demo</title>
Browser Events (// 6 <tle>
api.jquery.com/ 7 img {
8 height: 100px;
category/events/ 9 float: left;
browser-events/) 10 }
11 </tle>
Document Loading 12 <cript rc="http://code.jquer.com/jquer-3.7.0.j"
(//api.jquery.com/ 13 </head>
14 <od>
category/events/ 15
document- 16 <div id="image"></div>
loading/) 17
18 <cript>
Event Handler 19 (function() {
20 var flickerAPI = "http://api.flickr.com/ervice/feed
Attachment (// 21 $.getJON( flickerAPI, {
api.jquery.com/ 22 tag: "mount rainier",
category/events/ 23 tagmode: "an",
24 format: "jon"
event-handler- 25 })
attachment/) 26 .done(function( data ) {
27 $.each( data.item, function( i, item ) {
Event Object (// 28 $( "<img>" ).attr( "rc", item.media.m ).appendT
29 if ( i === 3 ) {
api.jquery.com/
30 return fale;
category/events/ 31 }
event-object/) 32 });
33 });
Form Events (// 34 })();
35 </cript>
api.jquery.com/
36
category/events/ 37 </od>
form-events/) 38 </html>

Keyboard Events
Demo:
(//api.jquery.com/
category/events/
keyboard-events/)

Mouse Events (//


api.jquery.com/
category/events/
mouse-events/)

Forms (//
api.jquery.com/
category/forms/)

Internals (//
Load the JSON data from test.js and access a name from the returned
api.jquery.com/
JSON data.
category/internals/)

5 of 14 9/10/23, 22:29
jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.getjson/

Manipulation (// 1 $.getJON( "tet.j", function( jon ) {


2 conole.log( "JON Data: " + jon.uer[ 3 ].name );
api.jquery.com/ 3 });
category/
manipulation/)
Load the JSON data from test.js, passing along additional data, and
Class Attribute (// access a name from the returned JSON data. If an error occurs, log an
api.jquery.com/ error message instead.
category/
manipulation/
1 $.getJON( "tet.j", { name: "John", time: "2pm" } )
class-attribute/) 2 .done(function( jon ) {
3 conole.log( "JON Data: " + jon.uer[ 3 ].name );
Copying (// 4 })
api.jquery.com/ 5 .fail(function( jqxhr, texttatu, error ) {
6 var err = texttatu + ", " + error;
category/
7 conole.log( "Requet Failed: " + err );
manipulation/ 8 });
copying/)

DOM Insertion,
Around (//
api.jquery.com/
category/
manipulation/
dom-insertion-
around/)

DOM Insertion,
Inside (//
api.jquery.com/
category/
manipulation/
dom-insertion-
inside/)

DOM Insertion,
Outside (//
api.jquery.com/
category/
manipulation/
dom-insertion-
outside/)

DOM Removal (//


api.jquery.com/
category/
manipulation/
dom-removal/)

DOM Replacement
(//api.jquery.com/

6 of 14 9/10/23, 22:29
jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.getjson/
category/
manipulation/
dom-
replacement/)

General Attributes
(//api.jquery.com/
category/
manipulation/
general-
attributes/)

Style Properties (//


api.jquery.com/
category/
manipulation/
style-properties/)

Miscellaneous (//
api.jquery.com/
category/
miscellaneous/)
Collection
Manipulation (//
api.jquery.com/
category/
miscellaneous/
collection-
manipulation/)

Data Storage (//


api.jquery.com/
category/
miscellaneous/
data-storage/)

DOM Element
Methods (//
api.jquery.com/
category/
miscellaneous/
dom-element-
methods/)

Setup Methods (//


api.jquery.com/
category/
miscellaneous/
7 of 14 9/10/23, 22:29
jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.getjson/
setup-methods/)

Offset (//
api.jquery.com/
category/offset/)

Properties (//
api.jquery.com/
category/properties/)
Properties of
jQuery Object
Instances (//
api.jquery.com/
category/
properties/jquery-
object-instance-
properties/)

Properties of the
Global jQuery
Object (//
api.jquery.com/
category/
properties/global-
jquery-object-
properties/)

Removed (//
api.jquery.com/
category/removed/)

Selectors (//
api.jquery.com/
category/selectors/)
Attribute (//
api.jquery.com/
category/
selectors/
attribute-
selectors/)

Basic (//
api.jquery.com/
category/
selectors/basic-
css-selectors/)

8 of 14 9/10/23, 22:29
jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.getjson/

Basic Filter (//


api.jquery.com/
category/
selectors/basic-
filter-selectors/)

Child Filter (//


api.jquery.com/
category/
selectors/child-
filter-selectors/)

Content Filter (//


api.jquery.com/
category/
selectors/content-
filter-selector/)

Form (//
api.jquery.com/
category/
selectors/form-
selectors/)

Hierarchy (//
api.jquery.com/
category/
selectors/
hierarchy-
selectors/)

jQuery Extensions
(//api.jquery.com/
category/
selectors/jquery-
selector-
extensions/)

Visibility Filter (//


api.jquery.com/
category/
selectors/visibility-
filter-selectors/)

Traversing (//
api.jquery.com/
category/traversing/)
Filtering (//

9 of 14 9/10/23, 22:29
jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.getjson/
api.jquery.com/
category/
traversing/
filtering/)

Miscellaneous
Traversing (//
api.jquery.com/
category/
traversing/
miscellaneous-
traversal/)

Tree Traversal (//


api.jquery.com/
category/
traversing/tree-
traversal/)

Utilities (//
api.jquery.com/
category/utilities/)

Version (//
api.jquery.com/
category/version/)
Version 1.0 (//
api.jquery.com/
category/version/
1.0/)

Version 1.0.4 (//


api.jquery.com/
category/version/
1.0.4/)

Version 1.1 (//


api.jquery.com/
category/version/
1.1/)

Version 1.1.2 (//


api.jquery.com/
category/version/
1.1.2/)

Version 1.1.3 (//


api.jquery.com/
category/version/

10 of 14 9/10/23, 22:29
jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.getjson/
1.1.3/)

Version 1.1.4 (//


api.jquery.com/
category/version/
1.1.4/)

Version 1.2 (//


api.jquery.com/
category/version/
1.2/)

Version 1.2.3 (//


api.jquery.com/
category/version/
1.2.3/)

Version 1.2.6 (//


api.jquery.com/
category/version/
1.2.6/)

Version 1.3 (//


api.jquery.com/
category/version/
1.3/)

Version 1.4 (//


api.jquery.com/
category/version/
1.4/)

Version 1.4.1 (//


api.jquery.com/
category/version/
1.4.1/)

Version 1.4.2 (//


api.jquery.com/
category/version/
1.4.2/)

Version 1.4.3 (//


api.jquery.com/
category/version/
1.4.3/)

Version 1.4.4 (//


api.jquery.com/
category/version/
1.4.4/)

11 of 14 9/10/23, 22:29
jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.getjson/

Version 1.5 (//


api.jquery.com/
category/version/
1.5/)

Version 1.5.1 (//


api.jquery.com/
category/version/
1.5.1/)

Version 1.6 (//


api.jquery.com/
category/version/
1.6/)

Version 1.7 (//


api.jquery.com/
category/version/
1.7/)

Version 1.8 (//


api.jquery.com/
category/version/
1.8/)

Version 1.9 (//


api.jquery.com/
category/version/
1.9/)

Version 1.11 & 2.1


(//api.jquery.com/
category/version/
1.11-and-2.1/)

Version 1.12 & 2.2


(//api.jquery.com/
category/version/
1.12-and-2.2/)

Version 3.0 (//


api.jquery.com/
category/version/
3.0/)

Version 3.1 (//


api.jquery.com/
category/version/
3.1/)

Version 3.2 (//

12 of 14 9/10/23, 22:29
jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.getjson/
api.jquery.com/
category/version/
3.2/)

Version 3.3 (//


api.jquery.com/
category/version/
3.3/)

Version 3.4 (//


api.jquery.com/
category/version/
3.4/)

Version 3.5 (//


api.jquery.com/
category/version/
3.5/)

Version 3.6 (//


api.jquery.com/
category/version/
3.6/)

Version 3.7 (//


api.jquery.com/
category/version/
3.7/)

BOOKS

Learning jQuery jQuery in Action jQuery Succinctly


Fourth Edition Bear Bibeault, Yehuda Cody Lindley
Karl Swedberg and Katz, and Aurelio De (https://
Jonathan Chaffer Rosa www.syncfusion.com/
(https:// (https:// ebooks/jquery)
www.packtpub.com/ www.manning.com/
web-development/ books/jquery-in-
learning-jquery- action-third-edition)
fourth-edition)

Copyright 2023 OpenJS Foundation (https://openjsf.org/) and


jQuery contributors. All rights reserved. See jQuery License
 Learning Center (https://learn.jquery.com/)

13 of 14 9/10/23, 22:29
jQuery.getJSON() | jQuery API Documentation https://api.jquery.com/jquery.getjson/
(https://jquery.org/license/) for more information. The
OpenJS Foundation (https://openjsf.org/) has registered
 Forum (https://forum.jquery.com/)
trademarks and uses trademarks. For a list of trademarks of
the OpenJS Foundation (https://openjsf.org/), please see our  Twitter (https://twitter.com/jquery)
Trademark Policy (https://trademark-policy.openjsf.org/) and
Trademark List (https://trademark-list.openjsf.org/).  IRC (https://irc.jquery.org/)
Trademarks and logos not indicated on the list of OpenJS
Foundation trademarks (https://trademark-list.openjsf.org/)  GitHub (https://github.com/jquery)
are trademarks™ or registered® trademarks of their
respective holders. Use of them does not imply any affiliation
with or endorsement by them. OpenJS Foundation Terms of
Use (https://terms-of-use.openjsf.org/), Privacy (https://
privacy-policy.openjsf.org/), and Cookie (https://
www.linuxfoundation.org/cookies) Policies also apply.

Web hosting by Digital Ocean (https://www.digitalocean.com) |


CDN by StackPath (https://www.stackpath.com)

14 of 14 9/10/23, 22:29

You might also like