我正在使用wordpress ajax調用來從wordpress主題functions.php中的函數返回簡單的內容。但是,將返回完整的html頁面。WordPress的Ajax從functions.php返回完整的HTML頁面,而不是回聲
這裏是Ajax調用
<?php
$ajax_nonce = wp_create_nonce("iwhq_beginner_select_course");
?>
<script type="text/javascript" language="javascript">
jQuery(document).ready(function(){
jQuery("#beg_golf_course").change(function() { //do this when course changes
//in Wordpress ajaxurl always points to admin-ajax.php
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
var course_id = 4;
//Do the ajax
jQuery.ajax({
type: "POST",
url: ajaxurl,
//NOTE - the action parameter calls the function in functions.php
data: { action: 'select_course_aj', course_id: course_id, _ajax_nonce: '<?php echo $ajax_nonce; ?>' },
//display alert on success
success: function(html){
alert(html);
}
}); //close jQuery.ajax(
return false;
});
});
</script>
這是
function select_course_func(){
echo $_POST["course_id"];
die();
}
add_action('wp_ajax_select_course_aj','select_course_func');
包含了jQuery AJAX調用頁面的HTML實際上是顯示在警報,而不是在functions.php中的作用回聲。
任何天才都能告訴我爲什麼?
感謝 馬克
我在函數.php中包含了select_course_func PHP調用中的nonce調用,但得到了相同的結果。 check_ajax_referer('iwhq_beginner_select_course','_ajax_nonce'); – Markol 2012-04-18 20:43:02
你看過Firebug中的請求/響應了嗎? – 2012-04-18 20:46:24
那麼究竟是什麼提醒?你期待'4'是否正確?最新發生的是你在加載和html 404頁面上使用html函數的div嗎?檢查您發送ajax呼叫的網址。像傑伊布蘭查德說,你可以很容易地做到這一點在螢火蟲或鍍鉻網絡標籤。 – Rooster 2012-04-18 21:35:05