简体   繁体   English

jQuery UI 自动完成不会显示返回 json (PHP)

[英]jQuery UI autocomplete wont display returned json (PHP)

The jQuery: jQuery:

$("[type=text]").autocomplete({
source: "json.php",
minLength: 0
}).addClass("ui-widget ui-widget-content ui-corner-left");

Works fine if I change the source: to a JS array.如果我将源更改为 JS 数组,则效果很好。 I know that the php is working because I can see it in the console, but here is the sample php anyways:我知道 php 正在工作,因为我可以在控制台中看到它,但无论如何这里是示例 php:

$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);

So the dropdown just isn't displaying.所以下拉菜单没有显示。 Feeling pretty stupid right about now.现在觉得自己好傻。

In your json.php file, be sure to set the content encoding to be json via the header() function before your echo.在您的 json.php 文件中,请务必通过 header() ZC1C42526474E683A94F1CAB 之前的 header() 将内容编码设置为 json This way jQuery should see the array as proper json.这样 jQuery 应该将阵列视为正确的 json。

header("Content-Type: application/json");
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);

http://jqueryui.com/demos/autocomplete/remote-jsonp.html http://jqueryui.com/demos/autocomplete/remote-jsonp.html

Check this get from demos site.从演示站点检查此获取。

$( "#city" ).autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: "http://ws.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: {
                        featureClass: "P",
                        style: "full",
                        maxRows: 12,
                        name_startsWith: request.term
                    },
                    success: function( data ) {
                        response( $.map( data.geonames, function( item ) {
                            return {
                                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                value: item.name
                            }
                        }));
                    }
                });
            },
            minLength: 2,
            select: function( event, ui ) {
                log( ui.item ?
                    "Selected: " + ui.item.label :
                    "Nothing selected, input was " + this.value);
            },
            open: function() {
                $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
            },
            close: function() {
                $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
            }
        });

A few days ago I was also having problems with a JSON-populated Autocomplete.几天前,我在使用 JSON 填充的自动完成时也遇到了问题。

My problem was that I wasn't setting the content-type, as Wally said above:我的问题是我没有设置内容类型,正如 Wally 上面所说的:

header("Content-Type: application/json");

I also wrapped my jQuery autocomplete call inside a getJSON, then used the data from that function to populate the autocomplete field.我还将 jQuery 自动完成调用包装在 getJSON 中,然后使用来自该 function 的数据填充自动完成字段。 My gut tells me that the extra getJSON shouldn't be necessary, but like you, I was having problems referencing my PHP file as the source.我的直觉告诉我不需要额外的 getJSON,但是像你一样,我在引用我的 PHP 文件作为源时遇到了问题。

$.getJSON("json.php", function(data) {
    $("[type=text]").autocomplete({
        dataType: "json",
        source: data,
        minLength: 1,
    }); 
});

Since I'm using an old PHP version, I hand-made my JSON.由于我使用的是旧的 PHP 版本,因此我手工制作了 JSON。 I included "label" and "value" fields because I'm fairly certain they're necessary for the autocomplete to work.我包括“标签”和“值”字段,因为我相当确定它们对于自动完成工作是必要的。

$jsonList = "[{"label" : "Item 1", "value" : "1"}, {"label" : "Item 2", "value" : "2"}]";
return $jsonList;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM