2012-08-11 24 views
-1
使用AJAX文章內容

我想在WordPress的獲得來自WordPress的

這裏通過AJAX得到帖子內容是JS部分

$("a.ajaxed").click(function(event) { 
    event.preventDefault(); 
    doAjaxRequest(); 
}); 
function doAjaxRequest(){ 
    jQuery.ajax({ 
     type: 'POST', 
     url: ajax_object.ajaxurl, 
     data: ({action : 'ajaxify', 
      post_id: $(this).attr('id') 
      }), 
     dataType: 'JSON', 
     success:function(data){ 
      console.log(data.post_title); 
     } 
    }); 
} 

這是functions.php的部分

function ajaxify() { 
    $post_id = $_POST['post_id']; 
    $post_data = get_post($post_id); 
    echo json_encode($post_data); 
} 

console.log(data.post_title)始終顯示未定義。

回答

0

POST_ID是未定義

$("a.ajaxed").click(function(event) { 
    event.preventDefault();  
    $.ajax({ 
     type: 'POST', 
     dataType: 'JSON', 
     url: ajax_object.ajaxurl, 
     data: ({ 
      action : 'ajaxify', 
      post_id: $(this).parent().attr("id") 
      }), 
     success:function(data){ 
      console.log(data); 
     } 
    }); 
});