Home > Article > Backend Development > PHP modularization for front-end ajax call implementation ajax php post jquery ajax php ajax points
Background: No php framework is used
Due to temporary needs, I need ajax to call methods in php. I simply wrote a php file. The file defines two methods. How to use ajax to call different methods of the same php file.
The following is the abc.php file. The two methods I defined, a method and b method
classabc
{functiona(){echo something;
}
functionb($args){echo something;
}
}
}
?>
The following is Controller.php. This file is a controller that calls other specific functional classes and plays a pivotal role, mainly through reflection. When implementing
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));
}
}
}
catch (Exception$exc) {
echo$exc->getTraceAsString();
}
}
?>
using ajax, use the following writing method to only call the a method in abc.php:
$.ajax({
type:"POST",
url: 'Controller.php?action=abc/a',
dataType: 'json',
success: function(data) { }
});
If you need to pass parameters, you can call it like this:
$.ajax({
type:"POST",
url: 'Controller.php',
data:{
action=abc/b,
参数1: 123,
参数2: 456}
dataType: 'json',
success: function(data) {
}
});
').addClass('pre-numbering' ).hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i ').text(i));
};
$numbering.fadeIn(1700);
});
});
The above introduces the implementation of PHP modularization for front-end ajax calls, including the content of ajax and PHP. I hope it will be helpful to friends who are interested in PHP tutorials.