2012-07-24 136 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