Wordpress Ajax-Js错误400(错误请求)

时间:2018-06-28 12:05:45

标签: php jquery ajax wordpress

它正在获取单词press ajax文件的链接,但给我一个错误的请求错误。

我收到的确切错误是POST http://[Website 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( '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;

});
});

0 个答案:

没有答案