How to make ajax call through wp-mvc plugin in wordpress

I am developing plugin with help wpmvc plugin.
I am facing problem to Ajax call.

I have added router.php file.

Read More
MvcRouter::admin_ajax_connect(array('controller' => 'tbl_projects', 'action' => 'show'));

I have create show() action in controller file and show.php file in view folder.
I have added js function in js file:

        url : ajaxurl,
        data : {
            action : 'tbl_projects_controller_show',
            postData : ''
        },
        dataType : "html",
        type : 'post',

I am getting ‘0’ response when the Ajax call

Related posts

Leave a Reply

3 comments

    1. In your-plugin/app/config/router.php file add:

      MvcRouter::admin_ajax_connect(array('controller' => 'adminprojects', 'action' => 'ajaxshow'));
      
    2. In your-plugin/controllers/admin/admin_projects_controller.php
      file add:

      class AdminProjectsController extends MvcAdminController {
          public function ajaxshow() {
              // also can use $_POST['content']
              echo 'GOT IT:' . $this->params['content'];
              die();
          }
      }
      

      Attention to: controller class name and controller variable pass in admin_ajax_connect function.

    3. Now you can use a code like this:

      jQuery(document).ready(function() {
      
          // Data to send to the AJAX call
          var data = {
              action: 'adminprojects_ajaxshow',
              content: 'Run from myplugin ajax'
          };
      
          // ajaxurl is defined by WordPress
          jQuery.post(ajaxurl, data, function (response) {
              // Handle the response
              console.log(response);
          });
      });