我一直在浏览类似的问题,还没有让这个功能发挥作用。我正在跟踪用户制作嵌入式vimeo视频并插入数据或更新数据的进度。
帖子页面上的JS:
$.ajax({
type: \'POST\',
dataType: \'json\',
url: ajaxurl,
data: {
\'action\': \'vimeo_progress\',
\'progress_percent\': progress,
\'progress_seconds\': seconds,
\'course_id\': courseID },
success : function(data) {
console.log(data); //FOR DEBUG
},
error : function(data, textStatus, XMLHttpRequest) {
console.log(\'updateProgress failed!\'); //FOR DEBUG
console.log(textStatus); //FOR DEBUG
}
});
功能。php
add_action( \'wp_ajax_nopriv_vimeo_progress\', \'store_vimeo_progress_callback\' );
function store_vimeo_progress_callback() {
// don\'t track if user is not logged in
if ( ! is_user_logged_in() )
return false;
$user_id = get_current_user_id();
$vimeo_percent = sanitize_text_field($_POST[\'progress_percent\']);
$vimeo_seconds = sanitize_text_field($_POST[\'progress_seconds\']);
$xxx_course_id = sanitize_text_field($_POST[\'course_id\']);
$table_name = $wpdb->prefix . "course_video_progress";
// Run WP query to retrieve user progress
$row = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $table_name WHERE user_id = %d AND course_id = %d;", $user_id, $xxx_course_id) );
if ($row) {
$wpdb->replace(
$table_name,
array(
\'id\' => $row->id,
\'user_id\' => $user_id,
\'course_id\' => $xxx_course_id,
\'progress_percent\' => $vimeo_percent,
\'seconds_played\' => $vimeo_seconds
),
array(
\'%d\',
\'%d\',
\'%f\',
\'%d\'
)
);
} else {
$wpdb->insert(
$table_name,
array(
\'user_id\' => $user_id,
\'course_id\' => $xxx_course_id,
\'progress_percent\' => $vimeo_percent,
\'seconds_played\' => $vimeo_seconds
),
array(
\'%d\',
\'%d\',
\'%f\',
\'%d\'
)
);
}
$response = array( \'success\' => true, \'data\' => \'hello\' ); //if $data is set
wp_send_json_success( $response );
exit;
}
不会将任何数据插入表中,也不会更新任何数据。我确实从回调中获得“成功”,但数据返回为0。
编辑:我刚刚意识到http://localhost/wordpress/wp-admin/admin-ajax.php 返回0,所以可能ajax调用甚至没有找到我的函数,只是返回0?