jQuery getJSON()

$.getJSON() requests JSON data with an HTTP GET request and parses the response before your callback runs. It is useful for lightweight APIs, lookup data and database-backed suggestion endpoints.

Basic Syntax Top ↑

$.getJSON(
    "json1.php",
    function ( data ) {
        console.log( data );
    }
);

Read Object Properties Top ↑

$.getJSON(
    "json1.php"
)
.done(function ( data ) {
    $( "#d1" ).empty();

    $.each(
        data,
        function ( key, value ) {
            $( "<div>" )
                .text(
                    key + " => " + value
                )
                .appendTo( "#d1" );
        }
    );
})
.fail(function () {
    $( "#d1" ).text(
        "Unable to load JSON data."
    );
});

Read an Array of Records Top ↑

$.getJSON(
    "json2.php"
)
.done(function ( response ) {
    $.each(
        response.data,
        function ( index, row ) {
            $( "<div>" )
                .text(
                    row.id + " | " +
                    row.name + " | " +
                    row.mark
                )
                .appendTo( "#d2" );
        }
    );
});

Read Records and Metadata Top ↑

$.getJSON(
    "json3.php"
)
.done(function ( response ) {
    console.log(
        response.data,
        response.value.message
    );
});

Pass Query Parameters Top ↑

$.getJSON(
    "student-data.php",
    {
        class: "Four",
        limit: 20
    }
).done(function ( rows ) {
    console.log( rows );
});

jQuery URL-encodes the data object. Avoid manually concatenating user input into the query string.

JSON from PHP Top ↑

header(
    'Content-Type: application/json; charset=utf-8'
);

echo json_encode(
    $data,
    JSON_UNESCAPED_UNICODE
);

See PHP JSON and JSON data formatting.

Live getJSON demo with three JSON shapes →

Download the original getJSON example ZIP






plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer