[英]Wordpress AJAX not sending and give admin-ajax.php error 400
I am trying to make an AJAX call on Wordpress but it is giving me an 400 error on admin-ajax.php.我正在尝试对 Wordpress 进行 AJAX 调用,但它在 admin-ajax.php 上给我一个 400 错误。 I think I have all the enqueues actions correctly setup.
我想我已经正确设置了所有入队动作。 Maybe I have still missed out one tiny error.
也许我仍然错过了一个小错误。 Please help.
请帮忙。
I have double-checked that the location of admin-ajax.php and the.js file are correct.我已经仔细检查了 admin-ajax.php 和 .js 文件的位置是否正确。 I have also tried enqueuing the.js file directly without registering it first, and calling admin-ajax.php directly at without the array.
我也尝试过直接将 .js 文件入队而不先注册它,并在没有数组的情况下直接调用 admin-ajax.php。
I have the following error: POST https: .../wp-admin/admin-ajax.php 400 jQuery.ajax({ <- this is the line where the error occurs).我有以下错误: POST https: .../wp-admin/admin-ajax.php 400 jQuery.ajax ({ <- 这是发生错误的行)。
The AJAX call has not been sent. AJAX 调用尚未发送。
functions.php功能。php
add_action( 'wp_enqueue_scripts', 'lp_load_scripts' );
function lp_load_scripts() {
wp_register_script( 'lp-script',get_stylesheet_directory_uri(). '/inc/js/admin.js', array('jquery'), NULL, true );
wp_enqueue_script('lp-script');
wp_localize_script( 'lp-script', 'lp_ajax', array('ajaxurl'=>admin_url( 'admin-ajax.php' )) );
}
add_action( 'wp_ajax_list_papers', 'list_papers' );
add_action( 'wp_ajax_nopriv_list_papers', 'list_papers' );
function list_papers() {
$data = $_POST['key'];
echo($data);
wp_die();
}
admin.js管理员.js
jQuery(document).ready(function($) {
jQuery('.find-paper').on('click',(e)=>{
var jsondata = JSON.stringify({
key: "listpapers",
urn: $('#urn').val(),
action: "list_papers",
});
console.log(jsondata);
jQuery.ajax({
url: lp_ajax.ajaxurl,
type: "post",
data: jsondata ,
success: function(response){
console.log(response);
},
error: function(e) {
console.log(e);
},
})
})
})
HTML HTML
<input type="text" name="urn" id="urn">
<input type="button" class="find-paper" id="find-paper" value="find papers">
OK.好的。 I finally solve the problem.
我终于解决了这个问题。
** Do NOT JSON.stringify the data object. ** 不要 JSON.stringify 数据 object。 **
**
Also, no need to add dataType: 'json' either.此外,也不需要添加 dataType: 'json' 。
Error 400 has now gone.错误 400 现已消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.