Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AJAX删除前端的WordPress帖子_Javascript_Php_Jquery_Ajax_Wordpress - Fatal编程技术网

Javascript 使用AJAX删除前端的WordPress帖子

Javascript 使用AJAX删除前端的WordPress帖子,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我正在尝试使用AJAX从前端删除WordPress帖子。 我的代码删除了帖子,但显示了带有“success”的空白页面,而我只想淡出这篇帖子,而不需要重新加载页面并显示空白页面 PHP代码: <?php if( current_user_can( 'delete_post' ) ) : ?> <?php $nonce = wp_create_nonce('my_delete_post_nonce') ?> <a href="<?

我正在尝试使用AJAX从前端删除WordPress帖子。 我的代码删除了帖子,但显示了带有“success”的空白页面,而我只想淡出这篇帖子,而不需要重新加载页面并显示空白页面

PHP代码:

<?php if( current_user_can( 'delete_post' ) ) : ?>
        <?php $nonce = wp_create_nonce('my_delete_post_nonce') ?>
        <a href="<?php echo admin_url( 'admin-ajax.php?action=my_delete_post&id=' . get_the_ID() . '&nonce=' . $nonce ) ?>" data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</a>
    <?php endif ?>
my_script.js代码:

jQuery( document ).ready( function($) {
    $(document).on( 'click', '.delete-post', function() {
        var id = $(this).data('id');
        var nonce = $(this).data('nonce');
        var post = $(this).parents('.post:first');
        $.ajax({
            type: 'post',
            url: MyAjax2.ajaxurl,
            data: {
                action: 'my_delete_post',
                nonce: nonce,
                id: id
            },
            success: function( result ) {
                if( result == 'success7' ) {
                    post.fadeOut( function(){
                        post.remove();
                    });
                }
            }
        })
        return false;
    })
})
问题是页面正在重新加载到带有“success”文本的空白页面,这时它应该淡出并从当前页面删除帖子,而不需要重新加载

看起来my_script.js根本没有被使用:(

非常感谢您的帮助

<a href="<?php echo admin_url( 'admin-ajax.php?action=my_delete_post&id=' . get_the_ID() . '&nonce=' . $nonce ) ?>" data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</a>
成功7而不是成功

不幸的是,点击链接或按钮后,使用#或制作按钮没有任何作用;/你知道为什么吗?它应该会起作用。看看这里:另外,你得到一个带有“成功”的“空”页面这一事实告诉我,你的ajax功能一般都在起作用。我想现在它只是一个打字错误:如果(result==“success7”)成功7而不是成功
<a href="<?php echo admin_url( 'admin-ajax.php?action=my_delete_post&id=' . get_the_ID() . '&nonce=' . $nonce ) ?>" data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</a>
<a href=# data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</a>
<button data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</button>
if( result == 'success7' )