Home >Web Front-end >JS Tutorial >Detailed explanation of ajax+php control function calling steps
This time I will bring you a detailed explanation of the steps for calling the ajax php control function. What are the precautions for calling the ajax php control function? The following is a practical case, let's take a look.
It is divided into 3 parts to complete the ajax calling logic of php. The following is the general structure.The first part: ajax request: mainly the action parameter, LoginController is the class name of php, login It is the function name in the LoginController class$('#submit').on('click', function (e) { e.stopPropagation(); $.ajax({ url: "../../controllers/Controller.php", data: { action: "LoginController/login", username: username, password: password }, dataType: "text", type: 'POST', timeout: 10000, error: function () { alert("服务器超时"); }, success: function (data) { alert(data); } }); });The second part: Controller.php, this file is the
<?php if (!empty($_REQUEST['action'])) { try { $action = explode('/', $_REQUEST['action']); $class_name = $action[0]; $method_name = $action[1]; require $class_name . '.php'; $class = new ReflectionClass($class_name); if (class_exists($class_name)) { if ($class->hasMethod($method_name)) { $func = $class->getmethod($method_name); $instance = $class->newInstance(); $func->invokeArgs($instance, array($_REQUEST)); $result = $instance->getResult(); echo $result; } } } catch (Exception $exc) { echo $exc->getTraceAsString(); } } ?>Part 3: LoginController.php, this file is a specific functional class
<?php class LoginController { private $result; function LoginController() { //初始化数据库连接等参数 } function login($args) { //具体的登录逻辑 } function getResult() { return $this->result; } } ?>I believe you have mastered the method after reading the case in this article. For more exciting content, please pay attention to php Chinese Other related articles online! Recommended reading:
JS implements ajax call background definition (with code)
Detailed explanation of the steps for ajax to implement waterfall flow layout (With code)
The above is the detailed content of Detailed explanation of ajax+php control function calling steps. For more information, please follow other related articles on the PHP Chinese website!