Wordpress Ajax Call返回0

时间:2014-04-17 08:00:57

标签: ajax wordpress

我正在使用一个(部分)停止工作的插件。作者并没有那么敏感,所以我开始挖掘自己。

我已经到了我认为我知道问题所在的部分:ajax调用一直返回0。

如果有人可以帮助我吗?

来自插件的ajax调用:

        var $this = $( this ),
        $parent = $this.parents( 'li' ),
        $container = $this.closest( '.rwmb-uploaded' ),
        data = {
            action: 'rwmb_delete_file',
            _ajax_nonce: $container.data( 'delete_nonce' ),
            post_id: $( '#post_ID' ).val(),
            field_id: $container.data( 'field_id' ),
            attachment_id: $this.data( 'attachment_id' ),
            force_delete: $container.data( 'force_delete' )
        };

    $.post( ajaxurl, data, function( r )
    {
        console.log(ajaxurl);
        console.log(data);
        console.log(r);
        if ( !r.success )
        {
            alert( r.data );
            return;
        }

        $parent.addClass( 'removed' );

        // If transition events not supported
        if (
            !( 'ontransitionend' in window )
            && ( 'onwebkittransitionend' in window )
            && !( 'onotransitionend' in myDiv || navigator.appName == 'Opera' )
        )
        {
            $parent.remove();
            $container.trigger( 'update.rwmbFile' );
        }

        $( '.rwmb-uploaded' ).on( 'transitionend webkitTransitionEnd otransitionend', 'li.removed', function()
        {
            $( this ).remove();
            $container.trigger( 'update.rwmbFile' );
        } );
    }, 'json' );

    return false;

console.log(r)返回0.其他两个日志填充正确的值。

ajax调用的php代码:

static function add_actions()
    {
        // Add data encoding type for file uploading
        add_action( 'post_edit_form_tag', array( __CLASS__, 'post_edit_form_tag' ) );

        // Delete file via Ajax
        add_action( 'wp_ajax_rwmb_delete_file', 'wp_ajax_delete_file' );
    }

static function wp_ajax_delete_file()
    {
        $post_id       = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
        $field_id      = isset( $_POST['field_id'] ) ? $_POST['field_id'] : 0;
        $attachment_id = isset( $_POST['attachment_id'] ) ? intval( $_POST['attachment_id'] ) : 0;
        $force_delete  = isset( $_POST['force_delete'] ) ? intval( $_POST['force_delete'] ) : 0;

        check_ajax_referer( "rwmb-delete-file_{$field_id}" );

        delete_post_meta( $post_id, $field_id, $attachment_id );
        $ok = $force_delete ? wp_delete_attachment( $attachment_id ) : true;

        if ( $ok )
            wp_send_json_success();
        else
            wp_send_json_error( __( 'Error: Cannot delete file', 'rwmb' ) );
    }

但似乎' wp_ajax_delete_file'永远不会被执行。

我现在正在查看此代码已有好几天没有找到解决方案。我不熟悉ajax,所以我不知道可能的解决方案的可能性。

如果您需要别的东西,我很乐意提供。

2 个答案:

答案 0 :(得分:0)

我认为你错过了这个钩子。这实际上适用于未登录的用户。

 add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );

答案 1 :(得分:0)

我做了一个干净的安装,它再次工作。我也认为我找到了源头。我从if循环中调用了ajax。那里的东西出了问题。如果我在循环外调用ajax,它就可以工作,即使对于内部调用也是如此。所以现在我以不同的方式实现了插件,这对我有用。

抱歉,我没有尽快发布,Stackoverflow也没有让我这么做。

相关问题