Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jquery自动完成中的JSON值格式不正确_Java_Jquery_Json_Autocomplete - Fatal编程技术网

Java jquery自动完成中的JSON值格式不正确

Java jquery自动完成中的JSON值格式不正确,java,jquery,json,autocomplete,Java,Jquery,Json,Autocomplete,我的jquery自动完成脚本有问题。 这是我的密码: 背 正面 来源:hotel-search.php返回[{label:A,id:1}] 当我将行source:hotel-search.php更改为source:[{label:A,id:1}] 它还不起作用 但是,当我将其更改为source:[{label:A,id:1}]时,它可以正常工作 我应该怎么做才能使hotel-search.php的返回像{label:hotel A,id:1}而不是{label:hotel A,id:1} Nau

我的jquery自动完成脚本有问题。 这是我的密码:

正面

来源:hotel-search.php返回[{label:A,id:1}]

当我将行source:hotel-search.php更改为source:[{label:A,id:1}]

它还不起作用

但是,当我将其更改为source:[{label:A,id:1}]时,它可以正常工作

我应该怎么做才能使hotel-search.php的返回像{label:hotel A,id:1}而不是{label:hotel A,id:1}


Naufal Abu Sudais:它还不起作用,指的是自动完成继续显示所有列表。它一直显示A,甚至我输入了B

使用autocomplete with AJAX调用时,会向服务发送一个GET参数,术语:

因此,如果您想要最短列表,必须在PHP文件中使用$_GET['term']来过滤响应项。

尝试$.getJSON,让jQuery获取JSON响应并将其解析为JSON对象:

$.getJSON('hotel-search.php', function(jsonData) {
   $("#hotel" ).autocomplete({
        position: { my : "right bottom", at: "right top" },
        source: jsonData,
        minLength: 2,
        select: function( event, ui ) {
            window.location.href=ui.item.id;
        }                   
    })._renderItem = function( ul, item ) {
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append($("<a></a>").text(item.label))   
            .appendTo(ul);
    };
});

如果您的问题与如何修改hotel-search.php返回的结果有关,那么您需要向我们显示该页面中的代码,而不是请求页面中的代码。{label:hotel A}不是有效的JSON。我怀疑任何图书馆都不会允许你这样做。顺便问一下:标准JSON有什么错误吗?你说它不起作用是什么意思?当你说它还不起作用时,有什么问题?你有什么错误吗?它还不起作用。自动完成会继续显示所有列表。它一直显示A,甚至我在演示中输入了B Look xhr响应…从一开始,当我使用它时,它就工作了。但是如果使用这种方法,自动完成需要很多时间等待后端首先生成查询。我的问题是:我应该怎么做才能使hotel-search.php的返回像{label:hotel A,id:1}而不是{label:hotel A,id:1}参见jquery演示并查找JSON响应:
<input type="text" id="hotel" />

<link rel="stylesheet" type="text/css" href="http://cdn0.favehotels.com/v2/style/autocomplete/jquery.ui.all.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>

<script type="text/javascript">                     
$(document).ready(function() {
    $( "#hotel" ).autocomplete({
        position: { my : "right bottom", at: "right top" },
        source: "hotel-search.php",
        minLength: 2,
        select: function( event, ui ) {
            window.location.href=ui.item.id;
        }                   
    })._renderItem = function( ul, item ) {
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append($("<a></a>").text(item.label))   
            .appendTo(ul);
    };
});
</script>
hotel-search.php?term=WhatYouTypeInAutocomplete
$.getJSON('hotel-search.php', function(jsonData) {
   $("#hotel" ).autocomplete({
        position: { my : "right bottom", at: "right top" },
        source: jsonData,
        minLength: 2,
        select: function( event, ui ) {
            window.location.href=ui.item.id;
        }                   
    })._renderItem = function( ul, item ) {
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append($("<a></a>").text(item.label))   
            .appendTo(ul);
    };
});