Jquery 自动完成 url 作为源 JSON

Jquery autocomplete with an url as source JSON

jQuery UI autocomplete with JSON from URL 我没有成功使用 jQuery 自动完成和 http://www.omdbapi.com/ 的 url 我跟着 jQuery UI 自动完成 JSON 来自 URL 我更改了参数,查询到 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([]);
            }
        });
    });
});

您不能直接使用它来填充列表。此 OMDb 服务 returns 每个请求一项。

在 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 );
      }
    });
  } );

你试过 ParametersBy Searchs 吗?因为您需要片名,但 t return 只有 title 的一部电影。另外写console.log(data);查看 data 是否被 return 编辑。

已更新

但是有一个问题,当结果过大时,出现omdbapi.comreturn错误信息,使用参数page

试试下面的代码

<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>