JQuery&AJAX在WordPress中从php获取数据。Admin-ajax.php返回400

时间:2021-10-31 作者:Manoj Madushantha

我在一家凉亭工作。js图形。我想要的是从php发送成员id查询,并将成员树json返回给jquery脚本。然后打印树
这是HTML表单

<form action="" method="post" id="blm_get_tree_view_meta_ajax_form" >           
    <?php echo $dropdown_html; ?>
    <input type="hidden" name="action" value="blm_member_tree_response">
    <input type="hidden" name="blm_mem_tree_meta_nonce" value="<?php echo $blm_add_meta_nonce ?>" />
    <input id="<?php echo $this->plugin_name; ?>-treeview_mem_id"  type="hidden" name="<?php echo "blm"; ?>[memid]" value="">
    <p class="submit"><input type="button" name="get_tree" id="get_tree" class="button button-primary" value="GET TREE"></p>
</form>
这是jquery脚本

<script>

jQuery( document ).ready( function( $ ) {
"use strict";
    
    $(\'#get_tree\').on(\'click\',function(){
        var user_id = $(\'#binary-level-marketting-treeview_mem_id\').val();

        //console.log(user_id);
        $.ajax({
            type:\'POST\',
            url: params.ajaxurl,
            data:{action:\'blm_member_tree_response\', user_id:user_id},
            success:function(data){
                if(data.status == \'ok\') {
                    console.log(data.nodes);
                    (function ($) {
                    var Renderer = function (canvas) {
                                var canvas = $(canvas).get(0);
                                var ctx = canvas.getContext("2d");
                                var particleSystem;
                                var that = {
                                    // arbor.js rendering goes here.
                                    
                                    //....
                                    //...
                                }         
                                    // and renderer
                                    $(document).ready(function () {
                            
                                        var data = {
                                            nodes: data.nodes,
                                            edges: data.edges
                                        }
                                        var sys = arbor.ParticleSystem(700, 700, 0.5);
                                        sys.parameters({ gravity:true});
                                        sys.renderer = Renderer("#viewport");
                                        sys.graft(data);
                            
                                    });
                    });
})(this.jQuery);
php响应函数为

public function getTreeGraphView() {
        
    if (isset($_POST[\'blm_mem_tree_meta_nonce\']) && wp_verify_nonce($_POST[\'blm_mem_tree_meta_nonce\'], \'blm_get_tree_view_meta_form_nonce\'))
    {
        if(!empty($_POST[\'user_id\'])){
            $data = array();
                    
            $mtree = new MemberTree();
            $mtree->getMemberTree($_POST[\'user_id\']);
            $nodes_ = $mtree->getNode();
            $edges_ = $mtree->getEdge();
            $parent_ = $mtree->getParent();
                    
            $data[\'nodes\'] = $nodes_;
            $data[\'edges\'] = $edges_;
                
            echo $data;
            die( \'0\' );
                
        }
        wp_die();

    } else {
        wp_die(__(\'Invalid nonce specified\', $this->plugin_name) , __(\'Error\', $this->plugin_name) , array(
            \'response\' => 403,
            \'back_link\' => \'admin.php?page=\' . $this->plugin_name.\'-product\',

        ));
    }
}
ajax操作

$this->loader->add_action( \'admin_ajax_blm_member_tree_response\', $plugin_admin, \'getTreeGraphView\');
将脚本和本地化按原样排队。

该过程99%成功,但管理效率很高。php返回400状态代码。有以下回应

还有0追加到输出。我在函数末尾插入wpdie()

enter image description here

我不能再解决这个问题了。这里有专家吗?

1 个回复
SO网友:While1

更改您的die( \'0\' );exit; 这对你来说很有用。

在wordpress中返回json的更好方法是wp_send_json( $data, 200 )

请在此处阅读更多信息:https://developer.wordpress.org/reference/functions/wp_send_json/

相关推荐

将boxtal PHP库集成到自定义WordPress插件中

我需要整合this PHP library 在我的WordPress网站上。我读到你必须创建一个插件或将其集成到子主题中。因此,我创建了一个插件,并将PHP库集成到插件文件夹中。https://ibb.co/vXfJjLj这是我的插件 <?php /* Plugin Name: igosen api boxtal Plugin URI: Description: Relier marketplace à boxtal Author: Serge As