1

Errors

I have a problem with using data of a json file in my html document.

At the top you can see the errors that i get.

I have this file created as a json file New.json :

[
    { "firstname":"Thomas", "lastname":"Anderson" },
    { "firstname":"Agent", "lastname":"Smith" },
    { "firstname":"Agent", "lastname":"Brown" }
]

I tried this code to get the data from my json file

    <script type="application/javascript">
            $(document).ready(function() {
                $("div").click(function () {
                    $.ajax({
                        type: 'GET',
                        url: './New.json',
                        dataType: 'json',
                        success: function(data){
                            window.alert(data);
                        },
                    });
                });
            });
    </script>

Thanks for your help :)

7
  • Show us your error message value! Dec 17, 2020 at 1:56
  • 2
    Your json file is invalid. Instead of surrounding the list of elements with {} which produces an invalid object, use [] to make a valid array.
    – James
    Dec 17, 2020 at 2:05
  • Thanks james fixed it, but it still does not work i edited my question now there is a screenshot of the error i become
    – Deniz
    Dec 17, 2020 at 2:23
  • Your Json file format not valid Dec 17, 2020 at 2:26
  • 1
    this is a CORS policy problem, you will find a ton of similar resolution here Dec 17, 2020 at 2:48

1 Answer 1

0

You URL might need to be: url: './New.json',

1
  • I changed it but it still does not work.
    – Deniz
    Dec 17, 2020 at 1:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.