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();
    
    评论

报告相同问题?