weixin_33730836 2015-08-21 11:52 采纳率: 0%
浏览 6

jQuery AJAX从PHP获取

I have this php and js file

PHP

<?php
    $user_id = '10';
    echo $user_id;
?>

JS

$(document).ready(function() {
            $.ajax({
                url:"uid.php",
                success:function(data){
                    alert(data);
}
});
});

I get "10" as alert

However for this PHP and JS there is no alert

PHP

<?php
class uidclass{
function uid_func($event, $arguments)
{
    $user_id = '10';
    echo $user_id;
}
}
?>

JS

$(document).ready(function() {
            $.ajax({
                url:"uid.php",
                success:function(data){
                    alert(data);
}
});
});

Thanks in advance

  • 写回答

1条回答 默认 最新

  • weixin_33704591 2015-08-21 11:56
    关注

    You need to use the class and method properly

    <?php
    class uidclass{
        function uid_func($event, $arguments) {
            $user_id = '10';
            echo $user_id;
        }
    }
    
    $bob = new uidclass;
    $bob->uid_func(null,null); // since the parameters are not defined
    

    or like this

    <?php
    class uidclass{
        function uid_func($event = null, $arguments = null) {
            $user_id = '10';
            return $user_id;
        }
    }
    
    $bob = new uidclass;
    print $bob->uid_func(); // we have already set defaults in the function definition
    

    or even add a constructor and print inside the method

    <?php
    class uidclass{
        public function uid_func($event = null, $arguments = null) {
            $user_id = '10';
            print $user_id;
        }
        public function __construct() {
            $this->uid_func();
        }
    }
    
    $bob = new uidclass();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中