使用url作为源JSON的Jquery自动完成

时间:2016-08-22 19:29:30

标签: php jquery json url jquery-ui-autocomplete

jQuery UI autocomplete with JSON from URL 我没有成功使用jQuery自动完成与来自http://www.omdbapi.com/的网址我遵循jQuery UI自动完成与URL中的JSON我更改了参数,查询到t和短语到标题,但它不起作用。你能救我吗?

$("#tags").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "http://www.omdbapi.com/",
            data: { t: request.term },
            success: function (data) {
                var transformed = $.map(data, function (el) {
                    return {
                        label: el.Title,
                        id: el.Years
                    };
                });
                response(transformed);
            },
            error: function () {
                response([]);
            }
        });
    });
});

2 个答案:

答案 0 :(得分:0)

您无法直接使用此功能填充列表。此OMDb服务每个请求返回一个项目。

在jsfiddle尝试了这个here

 $( function() {

    $( "#tags" ).autocomplete({
      source: availableTags
    });
    $( "#tags" ).autocomplete({
      source: function( request, response ) {
        $.ajax( {
          url: "//www.omdbapi.com/",
          dataType: "jsonp",
          data: {
            t: request.term,
            type: 'movie'
          },
          success: function( data ) {
             alert(JSON.stringify(data));
            // Handle 'no match' indicated by [ "" ] response
            response( data.length === 1 && data[ 0 ].length === 0 ? [] : data );
          }
        } );
      },
      minLength: 2,
      select: function( event, ui ) {
        log( ui.item ?
          "Selected: " + ui.item.value + " aka " + ui.item.id :
          "Nothing selected, input was " + this.value );
      }
    });
  } );

答案 1 :(得分:0)

您是否Parameters By Searchs一起尝试了?由于您需要标题,但t只能通过title返回一部电影。另外,写console.log(数据);是否返回data

<强>更新

但有一个问题,当结果太大时,omdbapi.com返回错误信息,请使用参数page

enter image description here 请尝试以下代码

<html>
<head>
    <script src='https://code.jquery.com/jquery-2.2.4.js'></script>
    <script src='https://code.jquery.com/ui/1.12.0/jquery-ui.js'></script>
    <link href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css" rel="stylesheet" media="screen" />
    <script type='text/javascript'>

    </script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#tags").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        method: "GET",
                        dataType: "json",
                        url: "http://www.omdbapi.com/?s="+request.term,
                        success: function (data) {
                            console.log(data);
                            // data.Search uses because we have `title`s in {"Search":[{..
                            var transformed = $.map(data.Search, function (el) {
                             return {
                             label: el.Title,
                             id: el.Years
                             };
                             });
                             response(transformed);
                        },
                        error: function () {
                            response([]);
                        }
                    });
                }
            });
        });
    </script>
</head>
<body>

  <div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags">
  </div>
</body>
</html>
相关问题