Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.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
Wordpress Ajax调用返回0_Ajax_Wordpress - Fatal编程技术网

Wordpress Ajax调用返回0

Wordpress Ajax调用返回0,ajax,wordpress,Ajax,Wordpress,我使用的插件(部分)停止工作。作者没有那么积极,所以我开始挖掘自己 我知道问题出在哪里:ajax调用始终返回0 如果有人能帮我 来自插件的ajax调用: var $this = $( this ), $parent = $this.parents( 'li' ), $container = $this.closest( '.rwmb-uploaded' ), data = { action: 'rwmb_de

我使用的插件(部分)停止工作。作者没有那么积极,所以我开始挖掘自己

我知道问题出在哪里: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;
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\u ajax\u delete\u file”似乎从未执行过

我现在研究这段代码好几天了,没有找到解决方案。我对ajax不是很熟练,所以我不知道可能的解决方案的可能性


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

我想你错过了这个机会。这实际上适用于未登录的用户

 add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );

我做了一个干净的安装,它再次工作。我还认为我找到了源头。我从if循环调用了ajax。里面出了点问题。如果我在循环外部调用ajax,它就工作了,甚至对于内部调用也是如此。所以现在我已经用一种不同的方式实现了这个插件,这对我来说很有用


很抱歉,我没有早点发布此消息,Stackoverflow不让我发布。

您必须在ajax函数的末尾使用die()函数停止返回0值。
exit()
也可以,它们是别名。这只适用于管理员,因此未登录的用户对这部分不重要。但我做了一个干净的安装,它再次工作。我还认为我找到了源头。我从if循环调用了ajax。里面出了点问题。如果我在循环外部调用ajax,它就工作了,甚至对于内部调用也是如此。所以现在我已经用一种不同的方式实现了这个插件,这对我来说很有用。但无论如何,谢谢你的帮助。