错误:'不是'in'(计算'b-1在a')'当使用jQuery-UI自动


error: 'not a valid argument for 'in' (evaluating 'b-1 in a')' when using jQuery-UI Autocomplete

一点背景:我正在为WordPress构建一个自动完成搜索字段,它将包含商品名称(一种层次自定义分类法,本质上是类别),但我认为这不再是WordPress的问题。我最初在这里问了一个问题,答案让我很好地理解了WP和AJAX是如何协同工作的。然后我阅读并整合了wp_localize_script,阅读了这个也帮助了我很多。

然而,我现在有一个控制台错误每当我试图搜索的东西:

TypeError: 'null' is not an object (evaluating 'a.length')
    map (jquery.js, line 2)
    (anonymous function) (autocomplete.js, line 5)
    j (jquery.js, line 2)
    fireWith (jquery.js, line 2)
    x (jquery.js, line 4)
    b (jquery.js, line 4)

错误的截图,希望能让它更清晰一些。

下面是我的PHP代码(在functions.php中):
wp_enqueue_script("autocomplete", get_template_directory_uri() . '/assets/js/autocomplete.js', array( 'jquery', 'jquery-form', 'json2' ), false, true );
wp_localize_script("autocomplete", "MyAjax_object", array(
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'myajax_nonce' => wp_create_nonce( 'myajax_nonce_val' ),
    'action' => 'myajax-submit'
));
function get_my_suggestions() {
    // This function should query the database and get results as an array of rows:
    // GET the recieved data: 'term' (what has been typed by the user)
    $term = $_GET['term'];
    $trades = get_terms( 'trade', array( 'fields' => 'names' ) );
    $tradesList = '["' . implode( '","', $trades ) . '"]';
    // echo JSON to page  and exit.
    $response = $_GET["callback"]."(". json_encode($trades) .")";  
    echo $response;  
    exit;
}
add_action( 'wp_ajax_myajax-submit', 'get_my_suggestions' );

和JavaScript (autocomplete.js):

$(document).ready(function() {
    $(".main-search-field").autocomplete({
    source: function( request, response ) {  
            jQuery.getJSON( MyAjax_object.ajaxurl + "?callback=?&action=myajax-submit", request, function( data ) {
                response( jQuery.map( data, function( item ) {
                    jQuery.each( item, function( i, val ) {
                        val.label = val.whatever; // build result for autocomplete from suggestion array data
                    } );
                    return item;
                } ) );
            } );  
        }, 
        minLength: 1,
        appendTo: ".search-container"
    }); 
});

有人知道为什么我得到这个错误吗?JavaScript加载得很好——它为建议列表添加了标记,但它是空的。此外,我注意到,每当我在输入框中搜索一些东西时,一个类ui-autocomplete-loading就会添加到输入框中,但在加载结果时,它永远不会消失。我不确定这是否重要,但它可能会帮助别人。

谢谢你的帮助:)

EDIT:刚才有人向我指出,在PHP中,我将错误的变量传递给json_encode。我已经修复了这个错误,现在错误如下:

[Error] TypeError: 'Accountants' is not a valid argument for 'in' (evaluating 'b-1 in a')
s (jquery.js, line 2)
each (jquery.js, line 2)
(anonymous function) (autocomplete.js, line 6)
map (jquery.js, line 2)
(anonymous function) (autocomplete.js, line 5)
j (jquery.js, line 2)
fireWith (jquery.js, line 2)
x (jquery.js, line 4)
b (jquery.js, line 4)

再次,这里是一个截图,使它更清楚。http://cl.ly/WbaN。我甚至不知道这个错误是什么意思,更不用说如何修复它了。

似乎问题在于您在get_my_suggestion()函数上构建响应的方式。

您应该构建一个类似于

的对象数组
[
 {
    value: "value1",
    label: "label1",
  },
  {
    value: "value2",
    label: "label2",
  },
]

因此,首先检查该函数的输出是否以这种方式格式化。