我不明白为什么我的代码不工作。我有一个标签wp_subscribe与id, user_id和post_id。我想在点击按钮后,通过ajax将这些数据添加到我的数据库。请看看我的代码。这是我在single.php中的html:
<button type="submit" name="subscribe" id="subscribe">Subscribe</button>
<input id="postId" type="hidden" value="<?php the_ID(); ?>" />
我subscribe.js :
onSubscribe: function() {
var $onSubscr = $('#subscribe');
$onSubscr.on('click', function() {
var $el = $(this),
post_id = $('#postId').val(),
$.ajax({
type:"POST",
url: admin_url.ajaxurl,
dataType:"json",
//data : 'action=subscribeOn&post_id='+post_id
data: {
action: 'subscribeOn',
post_id: post_id
},
cache: false
});
return false;
});
}
,显然也:
wp_register_script( 'subscribe', THEME_DIR . '/js/subscribe.js', array(), '', false );
wp_enqueue_script( 'subscribe' );
wp_localize_script( 'subscribe', 'admin_url', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ));
function subscribeOn() {
global $wpdb, $user_id, $post_id;
$user_id = get_current_user_id();
$post_id = $_POST['post_id'];
// $table_name = $wpdb->prefix . "subscribe";
$wpdb->insert("wp_subscribe", array( 'user_id'=>$user_id, 'post_id'=>$post_id), array('%s','%s'));
}
add_action( 'wp_ajax_nopriv_subscribeOn', 'subscribeOn' );
add_action('wp_ajax_subscribeOn', 'subscribeOn)');
谁能告诉我我的问题在哪里?在db中的wp_subscribe中什么也没有发生。我试了很多方法,但都没用。
对于javascript的调试,你需要习惯使用控制台(对于chrome,右键单击页面和"inspect element",你会看到控制台在弹出窗口的顶部。这将列出js的所有错误或输出。
jQuery和wordpress的一个常见错误是使用$
而不定义它,所以在使用$操作符引用jQuery或用$代替jQuery之前,您需要使用$='jQuery';
。查找无冲突jQuery获取更多信息。
www.yourserver.com/wp-admin/admin-ajax.php
, wp-admin文件夹当然可以在服务器上的其他地方。您应该使用console.log(admin_url.ajaxurl);
进行测试,输出将出现在前面提到的控制台中。更新的代码(不包装在一个函数和赋值给一个变量,只是使用它是。
$=jQuery;
$(document).on('click', '#subscribe', function() {
var post_id = $('#postId').val();
$.ajax({
type:"POST",
url: '/wp-admin/admin-ajax.php', // common error is ajax.url undefined (insert your home url into the start of the string, we will eliminate any possibility of mistake by using a static url.
//dataType:"json",
//data : 'action=subscribeOn&post_id='+post_id
data: {
action: 'subscribeOn',
post_id: post_id
},
success: function(data) {
console.log(data); //this writes any output from the server into the console, so if theres a respose you know ajaxurl and jquery is more or less correct, so its good to return something whilst testing...
},
cache: false
});
});
现在用于服务器。当测试时,你应该返回一些东西(你使用echo
var_dump
等输出的任何数据都将返回,当我们在浏览器代码中添加命令将其输出到控制台时,我们可以在那里检查它。
function subscribeOn() {
global $wpdb, $user_id, $post_id;
$user_id = get_current_user_id();
$post_id = $_POST['post_id'];
// we insert this to test the jQuery settings are correct. if one of the below does not appear in the console, we have issues in jQuery.
if($post_id):
echo $post_id;
else:
echo 'no post id is being submitted check selectors';
endif;
$success= $wpdb->insert("wp_subscribe",
array( 'user_id'=>$user_id, 'post_id'=>$post_id),
array('%s','%s')
);
if($success):
echo 'successfully inserted';
else:
echo 'not inserted, is the table name correct, etc? does the function work elsewhere?';
endif;
}
add_action( 'wp_ajax_nopriv_subscribeOn', 'subscribeOn' );
add_action('wp_ajax_subscribeOn', 'subscribeOn)');
希望这将排序。如果它击中了答案旁边的正确图标:)
我自己找到了正确的方法。只是因为subscribeOn后面有一个括号!
add_action('wp_ajax_subscribeOn', 'subscribeOn)');