php - PHP文件如何通过这个回调参数知道和处理一个函数?
问题描述
我正在寻找从 PHP 文件获取数据 JSON 的方法,而不知道 PHP 文件正在处理的函数。
<script>
function clickButton(){
var s = document.createElement("script");
s.src = "jsonp_demo_db.php?callback=myDisplayFunction";
document.body.appendChild(s);
}
</script>
解决方案
使用 get 请求发送函数名称并检查 Get 值是否存在和使用call_user_func($functionName)
,如果有函数参数可以使用call_user_func_array($functionName , $pramaters)
`function test()
{
echo "string";
}
if ($_GET['func']) {
$func = $_GET['func'];
call_user_func($func);
}`
您还可以使用 function_exists 来确保该函数是否存在并且此函数返回 False 或 True 此代码示例:
if(function_exists('my_function'))
{
throw new Exception("'my_function' is already defined!");
}
function my_function()
{
// Do the work here
}
推荐阅读
- javascript - 沙特阿拉伯移动和固定电话号码正则表达式验证
- delphi - Indy http 服务器会话不能正常工作
- ruby-on-rails - Mac Terminal 重新启动后无法运行大多数命令
- java - 我正在尝试用 Java 中的 RGB 中的每种颜色创建一个图像
- django - 如何在 Web 应用程序中显示最近的个性化项目?
- colors - 如何在 libgdx 中将背景颜色设置为白色?
- node.js - IBM Watson Assistant:如何启用用户指标
- ionic-framework - 触发离子cordova资源Android时如何解决此类错误
- c# - 在 DotNet Core 中单击 Html 按钮
- log4j - 为使用 slf4j 的外部库的日志配置 Liferay 日志系统