jquery & ajax getting data from php in wordpress . admin-ajax.php returns 400

Question

I am working on an arbor.js graph. What I want is to send member id query it from php return member tree json to again to jquery script. then print the tree.
here is HTML form

 <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>

And this is the jquery script

    <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 response function as

 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 action

 $this->loader->add_action( 'admin_ajax_blm_member_tree_response', $plugin_admin, 'getTreeGraphView');

enqueue scripting and localization did as it is.

The process is 99% success but admin-aax.php returns 400 status code. with following response

and also 0 append to output. I pit wpdie() at end of the function

enter image description here

I cant solve the issue anymore. any expert here?

0
Manoj Madushantha 1 month 2021-10-30T21:24:12-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse