weixin_33721344 2016-08-13 15:20 采纳率: 0%
浏览 37

Wiki API请求不起作用

I used some sample code from the Wiki API docs but when I enter a search item, nothing happens. No errors in the console, just nothing. The URL itself works if I enter it into a browser so I think something with the code is not passing in the proper value. Is there an issue with how I'm calling the API? Below is the relevant code:

$(document).ready(function(){
  $('#search-submit').click(function() {
      getWiki($('#searchVal').val());
    });
  /*add code for get lucky function*/
});

 function getWiki(searchParam) { 
   $.ajax( {
    url: 'http://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page='+searchParam+'&callback=?',
    dataType: 'json',
    type: 'POST',
    headers: { 'Api-User-Agent': 'Example/1.0' },
    success: function(data) {
       var result = data;
       pageTitle = result.title;
       $(".search-box").html(pageTitle);
    }
});
 };

Here is the HTML:

<body>
  <div class="container">
  <div class="col-lg-12 header">
    <h1>Search Wiki</h1>
  </div>
    <div class="row search-box">
      <div class="col-lg-10">
    <input placeholder=" Search" class="input" type="text" id="searchVal" name="searchVal"/></div>
      <div class="col-lg-2"><button type="submit" id="search-submit" name="search-submit" class="btn-default">
  <i class="fa fa-search fa-2x"></i>
</button></div>
    </div>
    <div class="row button-box text-center">
      <div class="col-lg-12">
        <button type="button" class="btn btn-primary" id="random">I'm Feeling Lucky</button></div>
    </div>
  </div>
</body>
  • 写回答

1条回答 默认 最新

  • weixin_33734785 2016-08-13 15:30
    关注

    The returned JSON has a parse property before the result, so it has to be data.parse.title to get the title etc.

    $(document).ready(function() {
        $('#search-submit').click(function() {
            getWiki($('#searchVal').val());
        });
        /*add code for get lucky function*/
    });
    
    function getWiki(searchParam) {
        $.ajax({
            url: 'http://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text&section=0&page=' + searchParam + '&callback=?',
            dataType: 'json',
            type: 'POST',
            headers: {
                'Api-User-Agent': 'Example/1.0'
            },
            success: function(data) {
                var result = data.parse;
                var pageTitle = result.title;
                $(".search-box").html('The title is : ' + pageTitle);
            }
        });
    };
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    Search for something : <input id="searchVal"><button id="search-submit">Search</button>
    <br /><br /><br />
    <div class="search-box"></div>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题