Home >Web Front-end >JS Tutorial >Detailed explanation of ajax+php control function calling steps

Detailed explanation of ajax+php control function calling steps

php中世界最好的语言
php中世界最好的语言Original
2018-04-25 15:54:512043browse

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

controller that calls other specific functional classes, and plays a pivotal role, mainly through Reflection to achieve

<?php
if (!empty($_REQUEST[&#39;action&#39;])) {
  try {
    $action = explode(&#39;/&#39;, $_REQUEST[&#39;action&#39;]);
    $class_name = $action[0];
    $method_name = $action[1];
    require $class_name . &#39;.php&#39;;
    $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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn