2012-08-11 47 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); 
     } 
    }); 
});