0

I am developing a WordPress plugin to do shortcode and view PowerBI reports. I have all data in the json file. I have tested the file and the function code. There is no issues with the WordPress codes. There is something wrong in the following AJAX, showing following error in console.

POST: http://localhost/xxx/wp-admin/admin-ajax.php [HTTP/1.1 400 Bad Request 418ms]

Here is the jQuery code:

jQuery(document).ready( function(){         
    jQuery('.subjectdiv').on('click', 'a.acaret', function(e) { 
        e.preventDefault();
        var rml_post_id = jQuery(this).attr( 'mydata' );    

        jQuery.ajax({
            url : mypowerbi_ajax.ajax_url,
            type : 'post',
            data : {
                action : 'powerbi_manager_ajax',
                _ajax_nonce: mypowerbi_ajax.check_nonce,
                post_id : rml_post_id
            },
            success : function( response ) {
              jQuery('.powerbitabwindow').html(response);
            }
        });
    });     
});

Here is the function and its code

function fn_powerbi_manager($atts) {
  wp_enqueue_style('bootstrapstyle', plugins_url('/powerbi_manager/assets/css/bootstrap.min.css'), __FILE__);
  wp_enqueue_script('bootstrapscript', plugins_url('/powerbi_manager/assets/js/bootstrap.min.js'), __FILE__);
  wp_enqueue_style('powerbistyle', plugins_url('/powerbi_manager/assets/css/style.css'), __FILE__);
  wp_enqueue_script('jqueryscript', plugins_url('/powerbi_manager/assets/js/jquery.min-v3.5.1.js'), __FILE__);
  wp_enqueue_script('powerbiscript', plugins_url('/powerbi_manager/assets/js/scripts.js'), __FILE__);

  add_action('wp_enqueue_scripts', 'bootstrapstyle');
  add_action('wp_enqueue_scripts', 'bootstrapscript');
  add_action('wp_enqueue_scripts', 'powerbistyle');
  add_action('wp_enqueue_scripts', 'jqueryscript');
  add_action('wp_enqueue_scripts', 'powerbiscript');
  
  wp_register_script( 'myajax-script', plugins_url( 'assets/js/ajaxcall.js', __FILE__ ), array('jquery'), null, true );
  wp_enqueue_script( 'myajax-script' );
  wp_localize_script( 'myajax-script', 'mypowerbi_ajax', array( 'ajax_url' => admin_url('admin-ajax.php'), 'check_nonce' => wp_create_nonce('powerbi_manager_nonce') ));

   require_once plugin_dir_path( __FILE__).'templates/frontend-page.php';
}
function my_powerbi_manager_ajax() 
{
    check_ajax_referer( 'powerbi_manager_nonce' );
     $rml_post_id = $_POST['post_id'];
     $json = file_get_contents(plugins_url('/powerbi_manager/json/powerbimanager.json'));
     $decoded_json = json_decode($json, true);
     $shortcodedata = array();
     foreach ($decoded_json as $rkey => $jsondata){
       if ($jsondata['visualidentifier'] == $rml_post_id){

           $shortcodedata[] = $jsondata['shortcode'];
       } 
    }
     echo do_shortcode($shortcodedata[0]);
}

if (function_exists('my_powerbi_manager_ajax'))
{
  add_action( 'wp_ajax_powerbi_manager_ajax', 'my_powerbi_manager_ajax' ); 
}
add_shortcode('powerbi-manager', 'fn_powerbi_manager');

Can anyone please find the error or give me suggestions? Thanks in Advance

1 Answer 1

1

Found it, I have logged out from the WordPress. I have added this code, now it is working,

add_action( 'wp_ajax_nopriv_powerbi_manager_ajax', 'my_powerbi_manager_ajax' ); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.