1

there is a problem when I'm trying to view a json file in a consloe(chrome developers tools) it says uncaught type error request.onload is not a function, but I can't find where is the problem can anybody help thanks in advace

<html>
<head>
  <title>ajax php</title>
  <!--<link rel="stylesheet" href="css/style.css">-->
</head>
<body>
  <header>
    <h1> json and ajax</h1>
    <button id="btn">fet info from JSON file</button>
  </header>

  <div id="animal-info"></div>

  <script src="main.js"></script>
</body>
</html>


<!--this code is in a seperate file main,js-->
    enter code here
    var request = new XMLHttpRequest();
    request.open('GET', 'https://learnwebcode.github.io/json-example/animals-1.json');
    request.onload() = function() {
      var data = request.responseText;
      console.log(request.responseText);
    };
    request.send();
  • You might find [this answer](http://stackoverflow.com/questions/9181090/is-onload-equal-to-readystate-4-in-xmlhttprequest#answer-9181508) helpful. – Mitya Jan 13 '17 at 16:18

2 Answers2

1

You are calling onload(), which isn't set yet. You should be setting it

request.onload = function() {
    var data = request.responseText;
    console.log(request.responseText);
};
Rhumborl
  • 16,349
  • 4
  • 39
  • 45
0

request.onload() is being called as a function in your code. Use the below code

request.onload = function(){
  //do something here
}