CONTEXT
我正在使用WordPress' suggest.js功能可在文本字段上创建自动提示功能。在字段中键入每个字符时,会向服务器发送一个Ajax查询,该服务器在SQL表中查询特定变量。响应应该是与查询对应的所有变量。
CODE
以下是代码:
/DISBLE UPDATE NOTIFICATIONS
add_action('wp_enqueue_scripts', 'se_wp_enqueue_scripts');
function se_wp_enqueue_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script('suggest');
}
add_action('wp_head', 'se_wp_head');
function se_wp_head() {
?>
<script type="text/javascript">
var se_ajax_url = '<?php echo admin_url('admin-ajax.php'); ?>';
jQuery(document).ready(function() {
jQuery('#se_search_element_id').suggest(se_ajax_url + '?action=se_lookup');
});
</script>
<?php
}
function se_lookup() {
global $wpdb;
$search = like_escape($_REQUEST['q']);
$query = 'SELECT DISTINCT meta_value FROM wp_usermeta
WHERE meta_key LIKE \'' . $search . '%\'
ORDER BY post_title ASC';
foreach ($wpdb->get_results($query) as $row) {
$meta_value = $row->meta_value;
// echo ' (' . $meta_value . ')' . "\n";
echo $meta_value;
}
die();
}
add_action('wp_ajax_se_lookup', 'se_lookup');
add_action('wp_ajax_nopriv_se_lookup', 'se_lookup');
RESULT
我收到200响应,好像查询工作正常。但是,响应内容为空。我希望echo $metavalue
能够返回我要查找的变量。但是,我尝试了echo "test"
,并返回空。我已经安装了Chrome扩展程序以查看Dev Tools中的Ajax调用/响应,这是我在响应中看到的内容:
response: Object
_transferSize: 515
bodySize: 33
content: Object
compression: -33
mimeType: "text/html"
size: 0
__proto__: Object
cookies: Array[0]
headers: Array[17]
headersSize: 482
httpVersion: "HTTP/1.1"
redirectURL: ""
status: 200
statusText: "OK"
__proto__: Object
startedDateTime: "2016-02-29T20:34:25.603Z"
time: 385.04700001794845
为什么响应内容对象不包含任何内容?