Php WordPressAjax-Js错误400(错误请求)
它得到了wordpressajax文件的链接,但是给了我一个错误的请求 im获取的确切错误是POST URL]/wp admin/admin-ajax.php 400(错误请求) Functions.php文件Php WordPressAjax-Js错误400(错误请求),php,jquery,ajax,wordpress,Php,Jquery,Ajax,Wordpress,它得到了wordpressajax文件的链接,但是给了我一个错误的请求 im获取的确切错误是POST URL]/wp admin/admin-ajax.php 400(错误请求) Functions.php文件 function as_custom_ajax_localizing() { wp_enqueue_script( 'custom.js', get_template_directory_uri() . '/guest-list/custom.js', array( 'jque
function as_custom_ajax_localizing() {
wp_enqueue_script( 'custom.js', get_template_directory_uri() . '/guest-list/custom.js', array( 'jquery' ), '1.0.0', true );
wp_localize_script( 'custom.js', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'as_custom_ajax_localizing' );
自定义php文件
add_action( 'wp_ajax_my_delete_post', 'as_delete_guest' );
function as_delete_guest(){
$permission = check_ajax_referer( 'as_delete_guest_nonce', 'nonce', false );
if( $permission == false ) {
echo 'error';
} else {
wp_delete_post( $_REQUEST['id'] );
echo 'success';
}
die();
}
自定义Js文件
$( document ).ready(function() {
$( ".remove_guest" ).click(function() {
var guestRowId = $(this).attr('id');
alert( "This id is " + guestRowId + " and has been clicked");
var nonce = $(this).data('nonce');
$.ajax({
type: 'post',
url: MyAjax.ajaxurl,
data: {
action: 'my_delete_post',
nonce: nonce,
id: guestRowId
},
success: function( result ) {
if( result == 'success' ) {
alert("Success");
}
else{
alert("problem");
}
}
})
return false;
});
});
为什么您不能尝试通过传递直接url而不是MyAjax.ajaxurl,请检查“MyAjax.ajaxurl”发出的警报您是否尝试过使用jQuery的.post()而不是.ajax()?另外,我知道WordPress ajax可能会很难调试,但您是否尝试过从as_delete_guest()输出一些非常简单的内容,以确保文件正常工作(没有nonce)?