Home>

As a practice of Ajax, we have implemented a function to output JSON data to the log when displaying html.

Applicable source code
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
  aaaaa
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="index.js"></script>
</body>
</html>
$.ajax({
  type:'GET',
  url:'json/data.json',
  dataType:'json',
  data: {name:'chara'},
  success: function (data) {
    console.log(data);
  }
});

The directory structure is as follows.

root/
├ json/
│ └ data.json
├ index.html
└ index.js

JSON data is not output to the log when html is displayed. I have confirmed that the js file can be read and that jQuery works.
What is the cause?

  • Answer # 1

    Speaking dare, 2 points

    One is the misunderstanding of ajax and the possibility of doing it locally rather than via http

    Since permission or path reference may be wrong, judge whether it can be read according to the same level without using json directory

    json data is incorrect = check by receiving an error with fail

    It's not a direct cause, but a point of concern

    json is probably static data but passing data is subtle

    Since success/error is an old way of writing, rewrite it as done/fail in the method chain.