Api给出相同的结果(纽约时报)

时间:2017-08-09 12:35:42

标签: javascript html api

这是JavaScript程序

        var api = 'https://api.nytimes.com/svc/search/v2/articlesearch.json?q=';
var Topic;
var apiKey = '&api-key=****';

function setup() {

  input = document.getElementById('topic').value;

}

function ArticleAsk(){

    var url = api + input.value() + apiKey;
    loadJSON(url, gotData);

}

function gotData(data){
    var articles = data.response.docs;

    for(var i = 0;i < articles.length; i++){
        createP(articles[i].snippet);
    }
}

这是HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>8.6_p5.js_api_query_user_input</title>
    <script src="libraries/p5.js" type="text/javascript"></script>

    <script src="libraries/p5.dom.js" type="text/javascript"></script>
    <script src="libraries/p5.sound.js" type="text/javascript"></script>
    <script src="myScript2.js" type="text/javascript"></script>

   <!--<script src="skect.js" type="text/javascript"></script>!-->
    <!-- <script src="myScript.js" type="text/javascript"></script>!-->
    <style> body {padding: 0; margin: 0;} canvas {vertical-align: top;} </style>
  </head>
  <body>

   <p>
      Topic: <input id ="topic" value="New"></input>
      <br/>
      <button onclick="ArticleAsk()" id="submit">submit</button>
    </p>
  </body>
</html>

现在的问题是,每次我去一个新话题时,它仍然给我相同的主题&#34; NEW&#34;默认情况下,有时它的&#34;值未定义&#34;虽然它是预建的方法。

1 个答案:

答案 0 :(得分:0)

通过编写input = document.getElementById('topic').valueinput已经是元素的值(字符串)。然后你调用未定义的input.value()。试试这个:

var input = document.getElementById('topic')

function ArticleAsk(){
    var url = api + input.value + apiKey;
    loadJSON(url, gotData);
}