weixin_33721427 2017-02-03 14:42 采纳率: 0%
浏览 29

预先输入自动完成AJAX

I'm trying to fill up the bootstrap autocomplete(Typeahead) list with data provided by an external Web-service, in this case "Wunderground Weather", but it's not working.

It's returnin an error saying "hasOwnProperty".

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

 <script src="bootstrap-typeahead.js"></script>

HTML

<div class="col-md-12">
    <h1>Search Dynamic Autocomplete using Bootstrap Typeahead JS</h1>   
    <input class="typeahead form-control"  type="text">
</div>

JQuery

$(".typeahead").typeahead({
   source: function (query,process) {
                return process(autoCompleteWunderGround(query))
            }
});


function autoCompleteWunderGround(query){
    var results = []
    $.ajax({
        url : "http://autocomplete.wunderground.com/aq?query=query",
        dataType : "jsonp",
        jsonp : "cb",
        data : {
        "query" : query,
        "format" : "JSON",
        },
        success : function(data) {
            $.each( data.RESULTS, function( index, value ){
                results.push(value.name) 
            })
        }
    });

    return results;

}

Thanks

  • 写回答

1条回答 默认 最新

  • weixin_33698823 2017-02-03 14:58
    关注

    I think the problem is in the return of the function autoCompleteWunderGround(). Ajax is asynchronous so the return of the function is executet before the success. The array results it's never filled. You can try with a callback function.

    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)