是否有人会指导此代码为何不起作用。甚至数据也会发生变化。这是我使用的ajax。
jQuery(document).ready(function($) {
$(".ue_ajax_enabled").on(\'click\', function(event) {
event.preventDefault();
/* Act on the event */
$.ajax({
url: ue_ajax.ajax_url,
type: \'POST\',
/*dataType: \'json\',*/
data: {
\'action\' : \'ue_ajax_enabled\',
\'new_username\' : $("#new_username").val(),
\'nonce\' : $("#new_username_nonce_check").val(),
},
beforeSend: function () {
$(".ue_ajax_enabled").text(ue_ajax.beforeMessage);
},
success: function (data) {
console.log(\'Success\');
console.log(data);
},
error: function (data) {
console.log(\'Error\');
console.log(data);
}
})
});
});
这是我使用的ajax动作挂钩。 add_action( "wp_ajax_ue_ajax_enabled", \'ue_ajax_enabled\' );
function ue_ajax_enabled() {
global $wpdb;
$currentUser = wp_get_current_user();
$user_id = $currentUser->ID;
if ( is_user_logged_in() && wp_verify_nonce( $_REQUEST[\'nonce\'], \'new_username_nonce\' ) && ! empty($_REQUEST[\'new_username\'])) {
// Get name of our users table
$tableName = $wpdb->prefix . \'users\';
if ( !username_exists( $_REQUEST[\'new_username\'] ) ) {
// Stripslashes and trim any whitespace, also replace whitespace with underscore
$newUsername = trim(str_replace(\' \', \'_\', stripslashes($_REQUEST[\'new_username\'])));
} else {
echo json_encode( array(\'username\' => \'Username exists, try any other\') );
die();
}
// Data to change
$dataToChange = array(\'user_login\' => $newUsername);
// Where to Change
$whereToChange = array(\'ID\' => $user_id);
// Change the data inside the table
$result = $wpdb->update($tableName, $dataToChange, $whereToChange, array(\'%s\'), array(\'%d\'));
if ($result) {
echo json_encode( array(\'update\' => true) );
} else {
echo json_encode( array(\'update\' => false) );
die();
}
if (ue_send_email_to_user()) {
$subject = \'Username Changed Successfully ID:\' . $user_id;
$message = "<p>You username has been successfully changed. Your new details are given below.</p>";
$message .= "<strong>Previous Username:</strong><span>{$currentUser->user_login}</span>";
$message .= "<strong>New Username</strong><span>{$newUsername}</span>";
$from = "From: " . get_bloginfo(\'name\');
wp_mail( array(ue_get_administrators_email(), $currentUser->email), $subject, $message, $from );
}
}
die();
}
代码在控制台中抛出错误消息,而不是前面设置的ajax的成功消息。任何关于为什么会发生这种情况的线索。提前感谢UPDATE: by commenting datatype the problem solves. But still it shows undefined when i access json.