2012-07-24 135 views
1

我在Wordpress中嘗試使用ajax請求通過傳遞用戶ID來獲取用戶數據。WordPress的 - 使用jQuery ajax獲取用戶信息POST請求

我可以看到,用戶ID通過AJAX POST正確發送,但我收到一個內部錯誤消息,我不知道爲什麼。

起初我以爲是因爲我試圖獲取一些自定義字段,我已經添加到用戶配置文件,但即使當我簡化了我的腳本我仍然收到錯誤消息。

任何幫助非常感謝!

前端

$('.author').click(function() { 

    var id = $(this).attr('id'); 
    var temp = id.split('-'); 
    id = temp[1]; 

    $.ajax({ 
      type: 'POST', 
      url: 'wp-content/themes/twentyeleven/author_info.php', 
      data: {id: id}, 
      dataType: 'html', 
      success: function(data) { 
       $('#author-bio').html(data);   
      } 
     });  

    return false; 
}); 

author_info.php

$user_id = $_POST['id']; 
$forename = get_the_author_meta('user_firstname', $user_id); 
$output = $user_id; 
echo $output; 

錯誤消息

500 (Internal Server Error) jquery.min.js:4

+0

這甚至不是在Wordpress中使用ajax的正確方法。你想看到正確的方法嗎? – Ohgodwhy 2012-07-24 15:14:23

+0

@Ohgodwhy爲什麼他會問這個問題是否得不到正確答案? – 2012-07-24 15:16:31

+0

@Jason Towne因爲'最佳實踐'!='它有效'。 – Ohgodwhy 2012-07-24 15:17:26

回答

2

Mathieu添加了一種可以攔截請求並重定向的方法,這很好。我更願意構建返回json_encoded數組的AJAX響應。

$('.author').click(function() { 

    var id = $(this).attr('id'); 
    var temp = id.split('-'); 
    id = temp[1]; 

    $.ajax({ 
    url: 'http://absolute.path/wp-admin/admin-ajax.php', 
    data: {'action' : 'ajax_request', 'fn': 'getAuthorMeta', 'id': id}, 
    dataType: 'json', 
    success: function(data) { 
     //We expect a JSON encoded array here, not an HTML template.  
    } 
    });  
    return false; 
}); 

現在我們構建出處理我們的ajax請求的函數。

首先,我們需要定義我們的AJAX ADD_ACTION方法 - >

add_action('wp_ajax_nopriv_ajax_request', 'ajax_handle_request'); 
add_action('wp_ajax_ajax_request', 'ajax_handle_request'); 

這裏我們需要同時使用add_action線。我不會理解爲什麼。你會在這裏注意到_ajax_request。這是我們在AJAX功能data: {'action' : 'ajax_request'}中發送的'操作'。我們使用這個鉤子來驗證我們的AJAX請求,它可以是任何你想要的。

接下來,我們需要構建或功能ajax_handle_request

function ajax_handle_request(){ 
    switch($_REQUEST['fn']){ 
    case 'getAuthorMeta': 
     $output = ajax_get_author_meta($_REQUEST['id']); 
     break; 
    default: 
     $output = 'That is not a valid FN parameter. Please check your string and try again'; 
     break; 
    } 
    $output = json_encode($output); 
    if(is_array($output)){ 
    return $output; 
    }else{ 
    echo $output; 
    } 
} 

現在讓我們來構建我們的函數,以真正獲得作者元。

function ajax_get_author_meta($id){ 
    $theMeta = get_the_author_meta([meta_option], $id); 
    return $theMeta; 
} 

其中[meta_option]是a field provided by WP's native get_the_author_meta function

此時,我們現在回到我們的success:function(data)和(數據)是我們已經返回的json_encoded數組的參考。我們現在可以遍歷該對象來獲取我們的字段並將它們輸出到頁面中,只要您願意。

+0

謝謝,儘管此時出現錯誤。對於clarfiy,'add_action'和'function ajax_handle_request(){'將被放置在'functions.php'中,而最後一個函數'function ajax_get_author_meta($ id){'應該放在'author_info.php'中? – martincarlin87 2012-07-24 15:42:45

+0

所有這些函數應該在functions.php中。沒有必要爲author_info.php。 – Ohgodwhy 2012-07-24 15:43:17

+0

好的,只是這樣做,我的網頁不會加載,只要最後一個功能粘貼到functions.php。另外,如果不需要單獨的ajax文件,爲什麼ajax請求的url仍然指向它? - 'url:'http:// absolute.path/wp-content/themes/twentyeleven/author_info.php',' – martincarlin87 2012-07-24 15:46:35

1

因爲您正在調用模板的特定頁面,該頁面可能與您博客中的任何文章不相符,所以您目前沒有進行POST。

相反,創建一個撐着,將做到這一點:

add_action('template_redirect', 'my_author_meta_intercept'); 
function my_author_meta_intercept(){ 
    if(isset($_POST['getAuthorMeta'])){ 
     echo get_the_author_meta('user_firstname', $_POST['getAuthorMeta']); 
     exit(); 
    } 
} 

這將短路請求相同的頁面,當您調用它使用前:

http://mysite/mycurrenturl?getAuthorMeta=testMetaKey 

因此調用該職位通常會像往常一樣返回文章,但是如果您傳入getAuthorMeta,它將停止選擇模板,並簡單地返回您希望返回的確切內容。

在你的頁面中,你只需要你的JavaScript更改爲:

$('.author').click(function() { 

    var id = $(this).attr('id'); 
    var temp = id.split('-'); 
    id = temp[1]; 

    $.ajax({ 
      type: 'POST', 
      url: window.location.href, 
      data: {getAuthorMeta: id}, 
      success: function(data) { 
       $('#author-bio').html(data);   
      } 
     });  

    return false; 
}); 

只要確保你適應的概念,你需要的東西!

+0

謝謝,我會給它一個鏡頭。 – martincarlin87 2012-07-24 15:26:24

+0

另請注意,您已使用「POST」作爲獲取信息的方法,請閱讀subjet,「POST」保留用於服務器上的狀態更改,而「GET」用於查詢信息。所以如果你想獲取數據,你應該總是在AJAX調用中使用GET。但是,如果您需要更改服務器的狀態,則應該是POST – 2012-07-24 15:28:24

2

我寧願建議您使用WP AJAX action method

在你的情況下,以下內容添加到您的functions.php文件。

add_action('wp_ajax_get_user_info', 'ajax_get_user_info'); 
add_action('wp_ajax_nopriv_get_user_info', 'ajax_get_user_info'); 

function ajax_get_user_info() { 
    //Handle request then generate response using WP_Ajax_Response or your html. 
} 

然後在JavaScript標記中。

$('.author').click(function() { 

    var id = $(this).attr('id'); 
    var temp = id.split('-'); 
    id = temp[1]; 

jQuery.post(
    ajaxurl, /* if you get error of undefined ajaxurl. set it to "http://example.com/wordpress/wp-admin/admin-ajax.php"*/ 
    { 
     'action':'get_user_info', 
     'user_id':id 
    }, 
    function(response){ 
     alert('The server responded: ' + response); 
    } 
); 
}); 

我建議您閱讀5 tips for using AJAX in WordPress

P.S;以上代碼未經測試,可能有錯誤。但你明白了。

+1

看到我的帖子爲這個長形式的版本! – Ohgodwhy 2012-07-24 15:35:17