使用ajax的同一代码的不同输出。为什么?

时间:2019-10-10 16:12:05

标签: javascript html ajax

<html>
    <head>

      <title>Book Library</title>
      <link
        rel="stylesheet"
        href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
        crossorigin="anonymous"/>
    </head>

    <body
      style=" font-family:Arial, Helvetica, sans-serif" >
      <div class="container">
        <h1
          class="text-center"
          style=" font-family:'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif; font-size: 50px;">
          Book Library
        </h1>
        <div style="height: 40px;">
          <input class=" w-75 h-100 pl-2  border border-primary rounded" type="text" name="letter"  id="letter" placeholder="Ex. English"/>
          <button class="btn btn-primary btn-lg" id="search">search</button>
        </div>
        <br />


      </div>
      <script
        src="https://code.jquery.com/jquery-3.4.1.min.js"
        integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
        crossorigin="anonymous"
      ></script>
      <script>

检查这部分代码:

          var books = [];
$.ajax({
url:
    "https://raw.githubusercontent.com/attainu/attainu-eagle/master/coding-challenges/week-5/day-3/books.json",
datatype: "json",
success: function(data) {
    data = JSON.parse(data);
   for(var i =0; i<data.length; i++){
       books.push(data[i]);
       }
}
});

**console.log(books)**;
console.log(typeof books);
var j = books[0];
   console.log(j);

$("#search").on("click", search); 
function search() {


var j = **books**;
  **console.log(j);**
   console.log(**typeof** j);


}
      </script>
    </body>

</html>

0 个答案:

没有答案