首页 文章

MVC控制器和ajax

提问于
浏览
0

嗨,我正在尝试学习MVC的基础,目前我没有使用oop而只是程序,不使用框架而不使用路由器 .

我需要使用ajax和jquery提交表单,问题是如果我将表单提交给控制器然后ajax返回整个页面,我怎么能只返回echo $ output;到ajax电话? . 我使用控制器来渲染视图,这是控制器 . 你建议只为ajax呼叫使用不同的控制器吗?

// Load settings files
require_once($_SERVER["DOCUMENT_ROOT"].'/config/load.php');
// Start session
session_start();

// Switch the view 
switch ($_SESSION['km-user-session']['km-user-role']) {

    case KM_ADMIN_ROLE:

        ob_start(); 
        // Load header for admin
        include_once(KM_ROOT_PATH.'/km-views/km-admin/km-header.php');
        $km_header = ob_get_contents(); 
        ob_end_clean(); 

            // Check if api is active and show different sidebar
            if($_SESSION['km-user-session']['km-api-active'] == true){

                ob_start(); 
                include_once(KM_ROOT_PATH.'/km-views/km-admin/km-api/km-sidebar.php');
                $km_sidebar = ob_get_contents(); 
                ob_end_clean(); 

            }else{

                ob_start();
                include_once(KM_ROOT_PATH.'/km-views/km-admin/km-sidebar.php');
                $km_sidebar = ob_get_contents(); 
                ob_end_clean(); 

            }

        // Load admin api receipts view
        include_once(KM_ROOT_PATH.'/km-views/km-client/km-api/km-receipts.php');
        break;

    case KM_CLIENT_ROLE:

        ob_start(); 
        include_once(KM_ROOT_PATH.'/km-views/km-client/km-header.php');
        $km_header = ob_get_contents(); 
        ob_end_clean(); 

            // Check if api is active and show different sidebar
            if($_SESSION['km-user-session']['km-api-active'] == true){

                ob_start(); 
                include_once(KM_ROOT_PATH.'/km-views/km-client/km-api/km-sidebar.php');
                $km_sidebar = ob_get_contents(); 
                ob_end_clean(); 

            }else{

                ob_start();
                include_once(KM_ROOT_PATH.'/km-views/km-client/km-sidebar.php');
                $km_sidebar = ob_get_contents(); 
                ob_end_clean(); 

            }


                if ($_SERVER["REQUEST_METHOD"] == "POST") {

            $startDate = $_POST['startDate'];
            $endDate = $_POST['endDate'];
            $clientCode = $_SESSION['km-user-session']['km-api-user-code'];
            $buildindCode = $_SESSION['km-user-session']['km-api-building-code'];

            $dates= array(

                'pk_prop' => $clientCode,
                'pk_cnd' => $buildindCode,
                'd_inizio' => $startDate,
                'd_fine' => $endDate
            );

            // cURL request to the receipts API
            $cURL = curl_init (KM_API);
            curl_setopt($cURL, CURLOPT_POST, 1);
            curl_setopt($cURL, CURLOPT_POSTFIELDS, http_build_query($dates)); 
            curl_setopt($cURL, CURLOPT_HEADER, 0);
            curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);

            $output = curl_exec($cURL);

                if (curl_error($cURL)) {
                    // Redirect to error 500 page and die
                    header('Location: '.KM_ERROR_500);
                    exit();
                }

            curl_close($cURL);

            echo $output;

        }

        ob_start(); 
        include_once(KM_ROOT_PATH.'/km-views/km-footer.php');
        $km_footer = ob_get_contents(); 
        ob_end_clean(); 

        // Load client api receipts view
        include_once(KM_ROOT_PATH.'/km-views/km-client/km-api/km-receipts.php');

        break;

}

这是我的jquery脚本

$(document).ready(function() {

      // Initialize air datepicker plugin
      $('.air-datepicker').datepicker();

      // Store form into variable
      var form= $("#requestForm");

      // Actions when form is submitted
      $('#submitForm').click(function(e) {

        // Ajax request
        $.ajax({

          type: "POST",
          data: form.serialize(),
          dataType:"html",

          success: function(result){

            // Reload the iframe with new content
            document.getElementById('tableContainer').contentDocument.location.reload(true);
            // Show the iframe
            $('#tableContainer').css('display','block');

            var $iframe = $('#tableContainer');

            $iframe.ready(function() {
                // append result to the iframe
                $iframe.contents().find("body").append(result);

            });


          },

          error: function(jqXHR, exception) {

            if (jqXHR.status === 0) {

              swal('Il server non risponde', 'Siamo spiacenti non è stato possibile eseguire questa operazione, per favore contatta l\'amministratore di sistema.', 'info');

            } else if (jqXHR.status == 404) {

              swal('Errore 404', 'Siamo spiacenti non è stato possibile eseguire questa operazione, per favore contatta l\'amministratore di sistema.', 'info');

            } else if (jqXHR.status == 500) {

              swal('Errore 500', 'Siamo spiacenti non è stato possibile eseguire questa operazione, per favore contatta l\'amministratore di sistema.', 'info');

            } else if (exception === 'parsererror') {

              swal('Si è verificato un errore!', 'Siamo spiacenti non è stato possibile eseguire questa operazione, per favore contatta l\'amministratore di sistema.', 'info');

            } else if (exception === 'timeout') {

              swal('Time Out', 'Siamo spiacenti non è stato possibile eseguire questa operazione, per favore contatta l\'amministratore di sistema.', 'info');

            } else if (exception === 'abort') {

              swal('Richiesta Annullata', 'Siamo spiacenti non è stato possibile eseguire questa operazione, per favore contatta l\'amministratore di sistema.', 'info');

            }


          }

        }); // Fine ajax


       e.preventDefault(); // Prevent form to be sent


      }); // fine submit form


    }); // fine document ready

1 回答

  • 1

    好吧,只要放一个骰子();在你的回声之后(“你的 Value ”);除了要返回到ajax结果的字符串之外,不要回显任何其他内容 .

相关问题