2
AJAXリクエストが成功したときにユーザー名を表示しようとしています。 私が問題になっているのは、名前が空白のままだということです。AJAXの成功関数内でユーザー名を取得する
簡体字コード(場合は削除文、小切手、および他の関係のないコード)私はこの
public function theme_addon_ajax_follow_me() {
...
$target_user = isset($_POST['data-follow-user']) ? $_POST['data-follow-user'] : false;
if(! empty($_POST['data-follow-user'])) {
$this->kiwi_follow_user($current_user, $target_user);
}
wp_die();
}
アップを使用してWPのAJAX機能インサイド
wp_localize_script('follow-me-ajax', 'ajax_setting', array(
'ajax_url' => admin_url('admin-ajax.php'),
'ajax_nonce' => wp_create_nonce('km-ajax-create-nonce'),
'ajax_follow_success' => $this->follow_me_success(),
));
AJAXコール
var user_to_follow = $('.km-author-follow a.km-meta-badge').attr('id');
$.ajax({
url : ajax_setting.ajax_url,
type : 'post',
data: {
action : 'theme_ajax_follow_me',
security : ajax_setting.ajax_nonce,
'data-follow-user' : user_to_follow,
},
success: function(data) {
$('.km-follow-me').html(ajax_setting.ajax_follow_success).hide().fadeIn('slow');
console.log(user_to_follow);
},
})
次;この機能はAJAXの$_POST['data-follow-user']
public function km_follow_me_author_name($args = array()) {
$author_info = get_userdata($args['follow_to']);
$author = $author_info->display_name;
return $author;
}
成功メッセージと、それがうまくいかないに基づいて、表示名をつかむために$target_user ID
public function kiwi_follow_user($current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'follow_to' => $user_to_follow
);
$response_success = $this->km_follow_me_author_name($args);
}
km_follow_me_author_name
に関数を送ります。
$name
は空白のままです。
public function follow_me_success() {
$name = $this->km_follow_me_author_name();
$content = sprintf(esc_html__('You\'re now following %s.', 'theme'), $name);
return $content;
}
ご協力いただきまして誠にありがとうございます。その後
$('.km-follow-me').html(data).hide().fadeIn('slow');
public function kiwi_follow_user
内部で私が使用:
if (! empty($response) && $response !== FALSE) {
$this->km_follow_me_success($user_to_follow);
}
そして
AJAX呼び出しの 'success:'で 'console.log(user_to_follow);'を呼び出すと、コンソールログはどうなりますか? –
正しいユーザーIDが表示されます。 – kiarashi
私はそれを理解したと思う。私は最初に自分のコードを徹底的にテストし、解決策を投稿します。 :) – kiarashi