我在wordpress中进行ajax调用,响应是由php函数生成的HTML标记。检查chrome上的元素时,ajax附加的结果顶部会出现一个空字符串。这是一个错误还是什么?
a ajax电话:
$.ajax({
url:"<?php echo admin_url(); ?>admin-ajax.php",
type:'POST',
data:'action=ms_selected_listings&parent=' + eduyear_val,
success:function(results){
$("#semester_selection ul").empty().append(results);
});
php函数:
function ms_selected_listings_response() {
if(isset($_POST['parent']))
{
$parent = $_POST['parent'];
ms_list_filtered(0, $parent);
die();
} // end if
}
另一个PHP函数:
function ms_list_filtered($included = 0, $parent = 0, $format = 'list', $type = 'echo', $current_cats = array()){
$cats= get_categories(array(
'include' => $included,
'orderby' => 'id',
'child_of' => '',
'parent' => $parent,
'hierarchical' => 0,
'hide_empty' => 0,
));
foreach($cats as $cat)
{
$cat_name = $cat->name;
$cat_id = $cat->term_id;
if($format == 'list') {
$output .= '<li><a href="" title="'.$cat_name.'" termid="'.$cat_id.'">'.$cat_name.'</a></li>';
}elseif ($format == 'option'){
if (in_array($cat_id, $current_cats)) {
$output .= '<option value="'.$cat_id.'" selected="selected">'.$cat_name.'</option>';
}else{
$output .= '<option value="'.$cat_id.'">'.$cat_name.'</option>';
}
}
}
if ($type == 'echo'){
echo $output;
}elseif ($type == 'return'){
return $output;
}
}