让JSON文件工作了一段时间,但后来又不再工作了

时间:2016-04-27 07:40:59

标签: jquery html json ajax parsing

首先,我能够收到JSON文件,但在做了一些事情之后,我无法记住收到JSON文件时收到错误。

我正在获取输入的值,然后调用一个url,接收此JSON文件,然后将其解析为表。

此页面在我收到错误消息后重新加载



function getPokemon() {
  var Value = $("#intvalue").val();
  var url = "http://pokeapi.co/api/v2/pokemon/" + Value + "/";
  //alert(url);      
  $.ajax({
    type: 'GET',
    url: url,
    //force to handle it as text
    dataType: 'text',
    success: function(data) {
      //data downloaded so we call parseJSON function 
      //and pass downloaded data
      var json = $.parseJSON(data);
      //now json variable contains data in json format
      //let's display a few items
      $('#ID').html(json.id);
      $('#name').html(json.name);
      $('#Experience').html(json.base_experience);
      $('#Height').html(json.height);
      $('#Weight').html(json.weight);
      $('#numAbility').html(json.abilities.length);
    },
    error: function() {
      alert("Unable to retreive JSON file");
      //console.log(data);

    }
  });
}

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="">
  <meta name="author" content="">
  <title>Pokedex Search</title>
  <link href="../../dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="../../assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
  <link href="dashboard.css" rel="stylesheet">

  <script src="../../assets/js/ie-emulation-modes-warning.js"></script>


</head>

<body>
  <nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="index.html#">Pokedex</a>
      </div>
      <div id="navbar" class="navbar-collapse collapse">
        <form class="navbar-form navbar-right">
          <input id="intvalue" type="text" class="form-control" placeholder="Enter a number between 1 and 100 to Search for your Pokemon..." style="width:800px">
          <button class="btn btn-primary" onclick="getPokemon()">SEARCH</button>
        </form>
      </div>
    </div>
  </nav>

  <div class="container-fluid">
    <div class="row">
      <div class="col-sm-3 col-md-2 sidebar">
        <ul class="nav nav-sidebar">
          <li class="active"><a href="index.html#">Overview <span class="sr-only">(current)</span></a>
          </li>

        </ul>

      </div>
      <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
        <h1 class="page-header">Dashboard</h1>

        <div class="row placeholders">
          <div class="col-xs-6 col-sm-3 placeholder">
            <img src="Pokémon_logo.png" width="1000" height="200" alt="Generic placeholder thumbnail">
          </div>
        </div>

        <h2 class="sub-header">Pokedox Search</h2>
        <div class="table-responsive">
          <table class="table table-striped">
            <thead>
              <tr>
                <th>#</th>
                <th>Name</th>
                <th>Experience</th>
                <th>Height</th>
                <th>Weight</th>
                <th>Number of Abilities</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td id="ID"></td>
                <td id="name"></td>
                <td id="Experience"></td>
                <td id="Height"></td>
                <td id="Weight"></td>
                <td id="numAbility"></td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script>
    window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')
  </script>
  <script src="../../dist/js/bootstrap.min.js"></script>
  <script src="../../assets/js/vendor/holder.min.js"></script>
  <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
</body>

</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:-1)

尝试将dataType文本更改为json(dataType:'json')