Javascript Wordpress admin ajax.php 400错误请求
事实证明,我正在制作一个Wordpress插件,其中我需要对Wordpress的文件“admin_AJAX.php”制作一个AJAX,但是在发送请求时,客户端给了我错误400(错误请求),我不明白是什么原因 捕获 Javascript:Javascript Wordpress admin ajax.php 400错误请求,javascript,php,ajax,wordpress,plugins,Javascript,Php,Ajax,Wordpress,Plugins,事实证明,我正在制作一个Wordpress插件,其中我需要对Wordpress的文件“admin_AJAX.php”制作一个AJAX,但是在发送请求时,客户端给了我错误400(错误请求),我不明白是什么原因 捕获 Javascript: $.post(window.dibibot.ajax_uri, { action: "dibibot_check_message_read", to: (window.dibibot.USER_KEYS.split(":")[1]).toSt
$.post(window.dibibot.ajax_uri, {
action: "dibibot_check_message_read",
to: (window.dibibot.USER_KEYS.split(":")[1]).toString(),
message_id: JSON.stringify(data.message)
}, function(response) {
console.log(response);
});
<?php
function dibibot_check_message_read() {
global $wpdb;
$conversation_guid = $_POST['to'];
$message_id = json_decode($_POST['message_id'], true);
$conversation = $wpdb->get_var("SELECT metadata FROM " . $wpdb->prefix . "dibibot_conversations WHERE guid = '".$conversation_guid."'");
$conversation = maybe_unserialize($conversation);
for ($j=0; $j < count($message_id) ; $j++) {
for ($i=0; $i < count($conversation); $i++) {
if($conversation[$i]['id'] == $message_id[$j]) {
$conversation[$i]['status'] = 2;
break;
}
}
}
$result = $wpdb->update($wpdb->prefix . 'dibibot_conversations', [ "metadata" => maybe_serialize($conversation) ], [ "guid" => $conversation_guid]);
echo $result ? 1 : 0;
wp_die();
}
?>
Ajax php函数:
$.post(window.dibibot.ajax_uri, {
action: "dibibot_check_message_read",
to: (window.dibibot.USER_KEYS.split(":")[1]).toString(),
message_id: JSON.stringify(data.message)
}, function(response) {
console.log(response);
});
<?php
function dibibot_check_message_read() {
global $wpdb;
$conversation_guid = $_POST['to'];
$message_id = json_decode($_POST['message_id'], true);
$conversation = $wpdb->get_var("SELECT metadata FROM " . $wpdb->prefix . "dibibot_conversations WHERE guid = '".$conversation_guid."'");
$conversation = maybe_unserialize($conversation);
for ($j=0; $j < count($message_id) ; $j++) {
for ($i=0; $i < count($conversation); $i++) {
if($conversation[$i]['id'] == $message_id[$j]) {
$conversation[$i]['status'] = 2;
break;
}
}
}
$result = $wpdb->update($wpdb->prefix . 'dibibot_conversations', [ "metadata" => maybe_serialize($conversation) ], [ "guid" => $conversation_guid]);
echo $result ? 1 : 0;
wp_die();
}
?>
代码注册Ajax处理程序,但当您仅在wp\u enqueue\u脚本上运行它时,已经太晚了,wp\u Ajax\u nopriv\u钩子已经运行 了解有关Wordpress Ajax的更多信息:
$.post(window.dibibot.ajax_uri, {
action: "dibibot_check_message_read",
to: (window.dibibot.USER_KEYS.split(":")[1]).toString(),
message_id: JSON.stringify(data.message)
}, function(response) {
console.log(response);
});
<?php
function dibibot_check_message_read() {
global $wpdb;
$conversation_guid = $_POST['to'];
$message_id = json_decode($_POST['message_id'], true);
$conversation = $wpdb->get_var("SELECT metadata FROM " . $wpdb->prefix . "dibibot_conversations WHERE guid = '".$conversation_guid."'");
$conversation = maybe_unserialize($conversation);
for ($j=0; $j < count($message_id) ; $j++) {
for ($i=0; $i < count($conversation); $i++) {
if($conversation[$i]['id'] == $message_id[$j]) {
$conversation[$i]['status'] = 2;
break;
}
}
}
$result = $wpdb->update($wpdb->prefix . 'dibibot_conversations', [ "metadata" => maybe_serialize($conversation) ], [ "guid" => $conversation_guid]);
echo $result ? 1 : 0;
wp_die();
}
?>
如果您在这里结束是因为收到400错误(错误请求),原因可能是在用户未登录时发出AJAX请求 您必须从更改代码
wp_ajax_my_action', 'my_action' );
到
也许它能帮助别人