【问题标题】:Getting response back from server but .html not sowing anything in wordpress ajax从服务器获取响应,但 .html 在 wordpress ajax 中没有显示任何内容
【发布时间】:2021-03-18 04:14:11
【问题描述】:

单击时,我将 id 作为数据发送,然后使用查询显示 WordPress 数据库中的用户名。我正在从服务器返回响应,但是在尝试使用 .html(response) 时没有添加。这可能与权限有关吗?就像只有管理员可以使用响应? 如果是这样的话,我能做什么。

这是ajax函数:

    function get_user_id() {
    let get_current_user_id =jQuery(this).attr('id');
    cur_user = '<?php echo get_current_user_id() ;?>';
    var postdata_name = {action: "incoming_user_name_ajax_call",
    param_user_to_chat: get_current_user_id,};

    jQuery.post(ajaxurl, postdata_name, function (response) {
    jQuery("#name-incoming-user").html(response);});

}

这是functions.php中的函数

  add_action("wp_ajax_incoming_user_name_ajax_call", "incoming_user_name_ajax_call_fn");
  add_action("wp_ajax_nopriv_incoming_user_name_ajax_call", "incoming_user_name_ajax_call_fn");


   function incoming_user_name_ajax_call_fn() {
   global $wpdb;
   $param_user_to_chat=isset($_REQUEST['param_user_to_chat'])?trim($_REQUEST['param_user_to_chat']):"";

   if (!empty($param_user_to_chat)) {

   $posts = $wpdb->get_results("SELECT distinct(display_name) FROM wp_users where  
   ID=$param_user_to_chat");
   echo $posts[0]->display_name;

  }
  wp_die();}

也为所有想知道什么的人发布 HTML jQuery(this).attr('id');是在做。根据点击,它的 id 为“4”或“2”。

<div id="target">
  <div class="user-list-hide" id="user-hide" style="display: block;">
   <div id="4"><span>Jason Bradley</span>/div>
   <div id="2"><span>John Saddington</span></div>
  </div>
</div>

【问题讨论】:

  • 嗨,这里的jQuery(this).attr('id'); 是什么?另外,你能看到回应吗?做alert(response)看看是否显示正确的数据。
  • 那么,响应成功返回了吗?
  • 我仍然想知道.. 你怎么能在这里得到正确的 id jQuery(this).attr('id') .. 因为没有 this 的参考。另外,this 可能会有所帮助。
  • @swati 因为我添加了 jQuery("#target").on("click", "div", get_user_id);我会检查你分享的链接。谢谢
  • jQuery("#target").on("click", "div", get_user_id); 更改为 jQuery("#user-hide").on("click", "div", get_user_id); 看看是否可行。

标签: ajax wordpress


【解决方案1】:

您的 jquery 代码中存在问题,即:您在这里调用 get_user_id() 函数的方式您使用了错误的选择器来获取您的内部 div。所以,让它工作改变:

jQuery("#target").on("click", "div", get_user_id); 

jQuery("#user-hide").on("click", "div", get_user_id);

【讨论】:

  • 是的,知道了。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
  • 2021-07-30
  • 1970-01-01
  • 2016-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多