【问题标题】:Jquery autocomplete with an url as source JSON使用 url 作为源 JSON 的 Jquery 自动完成
【发布时间】:2016-12-29 11:09:51
【问题描述】:

jQuery UI autocomplete with JSON from URL 我没有成功使用带有来自http://www.omdbapi.com/ 的 url 的 jQuery 自动完成我遵循了带有来自 URL 的 JSON 的 jQuery UI 自动完成我将参数、查询更改为 t 并将短语更改为 Title,但它不起作用。你能帮帮我吗?

$("#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([]);
            }
        });
    });
});

【问题讨论】:

  • 你在ajax中使用过method: "POST"dataType: "json"这两个选项吗?而且,你无法通过$_POST["t"] 捕捉到t 你能写出你的代码吗?
  • 我试过了,但它不起作用。这是我的简单测试的代码。
  • 那么,它成功了吗?

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


【解决方案1】:

您是否尝试通过ParametersBy Search 中使用s?因为你需要标题,但t 只返回一部title 的电影。另外,写console.log(data);查看data 是否返回。

更新

但是有一个问题,当结果太大时,omdbapi.com返回错误信息,使用参数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>

【讨论】:

    【解决方案2】:

    您不能直接使用它来填充列表。此 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 );
          }
        });
      } );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 2019-12-08
      相关资源
      最近更新 更多