MVC控制器和Ajax

时间:2018-12-09 14:44:04

标签: javascript php ajax

嗨,我正在尝试学习MVC的基础知识,此刻我不是在使用oop,而是在程序上,在框架和路由器之间。

我需要使用ajax和jquery提交表单,问题是如果我将表单提交给控制器,那么ajax将返回整个页面,我怎么能只返回echo $ output ;?到ajax电话? 。我也使用控制器来呈现视图,这就是控制器。您是否建议仅将其他控制器用于ajax调用?

#define _POSIX_C_SOURCE 200809L

这是我的jquery脚本

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

}

1 个答案:

答案 0 :(得分:1)

好吧,就放一个die();回声后(“您的值”);并且除了要返回到ajax结果中的字符串外,不要回显任何其他内容。