您好我在尝试使用一些wordpress ajax我的第一个ajax请求工作正常,但第二个没有。有人可以告诉我为什么会这样吗
工作正常:
PHP
//Add Students Details to DB
add_action( 'wp_ajax_my_action', 'my_action_callback' );
function my_action_callback() {
$fName = $_POST['fName'];
$lName = $_POST['lName'];
$dob = $_POST['dob'];
$gender = $_POST['gender'];
$diet = $_POST['diet'];
$current_id = get_current_user_id();
global $wpdb;
$result = $wpdb->insert(
'wp_students',
array(
'ID' => NULL,
'first_name' => $fName,
'last_name' => $lName,
'birthdate' => $dob,
'gender' => $gender,
'dietary_requirements' => $diet,
'user_id' => $current_id
)
);
echo json_encode($result);
wp_die();
}

JS
var data_value = {
action: 'my_action',
fName: $(acc[i]).find('input.fName').val(),
lName: $(acc[i]).find('input.lName').val(),
dob: $(acc[i]).find('input.dob').val(),
gender: $(acc[i]).find('select.gender').val(),
diet: $(acc[i]).find('textarea.diet_req').val()
};
$.ajax({
type: "post",
dataType: "json",
url: my_ajax_object.ajax_url,
data: data_value,
success: function(msg) {
if (msg == false) {
$('#insert_status').html('<strong><span style="color: red;">Error: </span></strong>Details Have Not Been Updated');
return false;
}
},
error: function(xhr, status, error) {
var i = JSON.parse(xhr.responseText)
alert(i.Message);
}
});
&#13;
不起作用:
PHP
//Delete Students Details
add_action( 'wp_ajax_my_delete', 'my_action_callback_delete' );
function my_action_callback_delete() {
echo 'here';
global $wpdb;
$result = $wpdb->delete( 'wp_students', array( 'usesr_id' => get_current_user_id()) );
echo json_encode($result);
wp_die();
}
&#13;
JS
var value = {
action: 'my_delete'
};
$.ajax({
type: "post",
dataType: "json",
url: my_ajax_object.ajax_url,
data: value,
success: function(msg) {
if (msg == false) {
$('#insert_status').html('<strong><span style="color: red;">Error: </span></strong>Details Have Not Been Updated');
return false;
}
},
error: function(xhr, status, error) {
var i = JSON.parse(xhr.responseText)
alert(i.Message);
}
});
&#13;
如果你能帮助我会很棒。 Wordpress Ajax并不像普通的ajax那样直截了当。
答案 0 :(得分:1)
你的删除功能中是否只有拼写错误?
'usesr_id' => get_current_user_id()
应该是
'user_id' => get_current_user_id()
//Delete Students Details
add_action( 'wp_ajax_my_delete', 'my_action_callback_delete' );
function my_action_callback_delete() {
echo 'here';
global $wpdb;
$result = $wpdb->delete( 'wp_students', array( 'usesr_id' => get_current_user_id()) );
echo json_encode($result);
wp_die();
}
&#13;