A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

How to Load Local JSON File Using jQuery
Q:

How to Load Local JSON File Using jQuery

0

How to Load Local JSON File Using jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this 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}.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now