WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES
Advertisements

How to Load Local JSON File Using jQuery

Topic: JavaScript / jQueryPrev|Next

Answer: Use the jQuery $.getJSON() Method

You can simply use the $.getJSON() method to load local JSON file from the server using a GET HTTP request. If the JSON file contains a syntax error, the request will usually fail silently.

Let's try out the following example to understand how it basically works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Loading Local JSON File</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head> 
<body>
    <script>
    $(document).ready(function(){
        $.getJSON("test.json", function(data){
            console.log(data.name); // Prints: Harry
            console.log(data.age); // Prints: 14
        }).fail(function(){
            console.log("An error has occurred.");
        });
    });
    </script> 
</body>
</html>

The "test.json" file contains a simple JSON string {"name": "Harry", "age": 14}.


Related FAQ

Here are some more FAQ related to this topic:

Advertisements
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties