如何在单个入口点发送Ajax

时间:2018-09-12 07:45:22

标签: php jquery .htaccess datatables

有这样一个问题,有必要在加载视图中发送ajax,但是由于创建了输入的统一点,因此无法指定地址。

某些代码:.htaccess:

AddDefaultCharset UTF-8
Options -Indexes
Options +FollowSymLinks
RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

写出最简单的路由器,它扫描$ _SERVER ['REQUEST_URI']并将其切成几部分。示例代码发布时间太长,但是我会尝试的。 /app/core/route.php:

<?php
class Route
{
    static function start() 
    {
        $controller_name = 'Main';
        $action_name = 'index';
        $params = null;
        $routes = explode('/', $_SERVER['REQUEST_URI']);
        if (!empty($routes[2])){ #
            $controller_name = $routes[2];
        }
        if (!empty($routes[3])){ 
            $action_name = $routes[3];
        }
        if (!empty(array_slice($routes, 4))) { 
            $params = array_slice($routes, 4);
        }

        $model_name = 'Model_' . $controller_name; 
        $controller_name = 'Controller_' . $controller_name; 
        $action_name = 'action_' . $action_name; 
        $model_file = mb_strtolower($model_name) . '.php'; 
        $model_path = "app/models/" . $model_file; 
        if (file_exists($model_path)) {
            include "app/models/" . $model_file;
        }
        $controller_file = mb_strtolower($controller_name) . ".php"; 
        $controller_path = "app/controller/" . $controller_file; 
        if (file_exists($controller_path)) { 
            include "app/controller/" . $controller_file;
        } else { 
            Route::ErrorPage404();
        }
        if (!class_exists($controller_name)) { 
            Route::ErrorPage404();
        } else { 
            $controller = new $controller_name;
            $action = $action_name;
            if (method_exists($controller, $action)) {
                if ($controller_name != 'Controller_auth') {
                    if ($controller->check_login_status()) {
                        if ($params) {
                            $controller->$action($params);
                        } else {
                            $controller->$action();
                        }
                    }

                } else {
                    if ($params) {
                        $controller->$action($params);
                    } else {
                        $controller->$action();
                    }
                }

            } else {
                Route::ErrorPage404();
            }
        }


    }   

    function ErrorPage404()
    {
        $host = 'http://' . $_SERVER['HTTP_HOST'] . '/super_crm/';
        header('HTTP/1.1 404 Not Found');
        header("Status: 404 Not Found");
        header('Location:' . $host . '404'); #редирект на контроллер 404
    }

    function AuthPage()
    {
        $host = 'http://' . $_SERVER['HTTP_HOST'] . '/super_crm/main';
        header('HTTP/1.1 404 Not Found');
        header("Status: 404 Not Found");
        header('Location:' . $host); 
    }

}

处理和结果在控制器应用程序/控制器/ controller_join.php中输出:

function action_test() { 
        $ajax_test = $this->model->ajax_test();
        $ajax_json = json_encode($ajax_test, JSON_UNESCAPED_UNICODE);
        //header('Content-Type: application/json; charset=UTF8');
        return ($ajax_json);
}

也尝试通过这种方式得出:

function action_test() { 
        $ajax_test = $this->model->ajax_test();

        $this->view->generate('join/_search_result.php', 'join/_search_result.php',
                                array(
                                        'title' => 'Заявки на подключения!',
                                        'objects' => $ajax_test
                                     )
                              );
    }   

因此,当您直接转到页面时,join / test会生成正确的json(在视图中进行json的转换)

AJAX本身需要在此请求中发送,问题的实质是如何正确指定将请求发送到的地址(如果您指定/ join / test(获取错误404),我希望它是纠正.htaccess所必需的,以便可以将我的index.php定向到文件发送请求,而对此稍为陌生,我想了解是否有更简单的解决方案,我将不胜感激。

jQuery本身:

$(document).ready(function() {
    $('#main_table').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "join/test"
    } );
} );

0 个答案:

没有答案