繁体   English   中英

发送用于Ajax请求的HTML格式不起作用

[英]Html format to send for ajax request not working

我试图在将HTML格式发送到ajax请求之前获取它的html格式。但是发生了这样的错误: Error: SyntaxError: Unexpected token < - parsererror - [object Object]

我只想通过数组中的值进行选择,这是代码:

$args = array(
    'type'        => 'ad_listing',
        'child_of'    => $parent_cat,
        'orderby'     => 'name',
        'order'       => 'ASC',
        // 'hide_empty'  => 1,
        // 'hierarchical'=> 1,
        'exclude'     => '',
        'include'     => '',
        'number'      => '',
        'taxonomy'    => 'ad_cat',
        'pad_counts'  => false 
);

$results = get_categories( $args );

$result = '<select>';
foreach ($results as $key => $value) {

    $result .= '<option>' . $value . '</option>';

}
return $result .= '</select>';

// return the result to the ajax post
die( json_encode( array( 'success' => true, 'html' => $result ) ) );

$result是我要发送的html格式。这里出现错误: Error: SyntaxError: Unexpected token < - parsererror - [object Object]语法错误Error: SyntaxError: Unexpected token < - parsererror - [object Object]

但是,如果我以这种方式使用代码,则会得到数组对象:

    $args = array(
    'type'        => 'ad_listing',
        'child_of'    => $parent_cat,
        'orderby'     => 'name',
        'order'       => 'ASC',
        // 'hide_empty'  => 1,
        // 'hierarchical'=> 1,
        'exclude'     => '',
        'include'     => '',
        'number'      => '',
        'taxonomy'    => 'ad_cat',
        'pad_counts'  => false 
);

$result = get_categories( $args );


// return the result to the ajax post
die( json_encode( array( 'success' => true, 'html' => $result ) ) );

我确定我做错了:(

我假设这是一个WordPress问题,get_categories返回对象数组( https://codex.wordpress.org/Function_Reference/get_categories )。

请执行下列操作:

foreach($results as $cat) {
$result .= '<option>'.$cat->name.'</option>';
}

...或根据WordPress Codex文档的需要。

暂无
暂无

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

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