This article mainly introduces ajax + php to control all background function calls. For more information, see
This article mainly introduces ajax + php to control all background function calls. For more information, see
A total of three parts are used to complete the ajax call logic of php. The following is a rough structure:
Part 1: ajax request: mainly the parameter action. LoginController is the php class name, and login 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 ("server timeout") ;}, success: function (data) {alert (data );}});});
Part 2: Controller. php. This file is used to call controllers of other specific functional classes and plays a pivotal role, mainly through reflection.
<? Phpif (! Empty ($ _ REQUEST ['action']) {try {$ action = explode ('HTTP: // www.jb51.net/', $ _ 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 function class.
<? Phpclass LoginController {private $ result; function LoginController () {// initialize database connection and other parameters} function login ($ args) {// specific login logic} function getResult () {return $ this-> result ;}}?>
This is all about this article. I hope you will like it.