Wordpress在ajax调用时返回0

时间:2013-08-02 10:08:19

标签: ajax wordpress

我遇到问题,Wordpress总是在这个ajax调用上返回0。谁能告诉我我做错了什么?

的Ajax:

$j.ajax({  
       type: 'POST',  
       url: 'http://localhost/all-in-one/wp-admin/admin-ajax.php',  
      data: {  
         action: 'header_galley_ajax',
         id: '1'
       },  
      success: function(data, textStatus, XMLHttpRequest){  
        alert (data); 
       },  
     error: function(MLHttpRequest, textStatus, errorThrown){  
         alert('error:'+errorThrown);  
     }  
     });

的functions.php:

function header_galley_ajax () {
    $id = $_POST['id'];
    echo "ajax fired:".$id;
    die();
}
add_action( 'wp_ajax_nopriv_ header_galley_ajax', 'header_galley_ajax' );  
add_action( 'wp_ajax_ header_galley_ajax', 'header_galley_ajax' ); 

1 个答案:

答案 0 :(得分:1)

您需要删除'wp_ajax_nopriv_ header_galley_ajax'和'wp_ajax_ header_galley_ajax'中的空格:

add_action( 'wp_ajax_nopriv_header_galley_ajax', 'header_galley_ajax' );  
add_action( 'wp_ajax_header_galley_ajax', 'header_galley_ajax' );