通过AJAX在Javascript中调用PHP函数的麻烦

I am trying to use Ajax to implement a php function in my html file (that contains javascript). I am trying to get all files locally that are JSON files. I tried my best to follow this: http://tinyurl.com/n7zttd9 but I am having trouble. For some reason, I am getting undefined in my alert statement (which is the closest thing I could get to a print statement so far, but if there are other options to print, I am very open to them). Here is some of my code for my local html file:

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code

function AjaxCaller(){
    var xmlhttp;
    try{
        // for firefox, safari, opera
        xmlhttp = new XMLHttpRequest();
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
            try{
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }catch(E){
                xmlhttp = false;
                alert("Your browser broke!");
            }
        }
    }

    if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

function callPage(url, div){
    ajax=AjaxCaller();
    ajax.open("GET", url, true);

    ajax.onreadystatechange=function(){
        if(ajax.readyState==4){
            if(ajax.status==0){
                div.innerHTML = ajax.responseText;
            }
        }
    }
    ajax.send(null);

}
//-->
</script>

<script id='code-js' type="text/javascript">
/*...*/
function load(){


    var jsonstuff = callPage('findjson.php', document.getElementById('checkjson'));
    alert(jsonstuff);

    Scene.loadObject(jsonstuff);

}
/*...*/
</script>

Here is my php code (file is findjson.php locally)

<?php
function checkjson() {
    foreach (glob("*.json") as $filename) {
        return $filename;
    }
?>

I used an ajax.status code of 0 because I found online that it has to be 0 for locally. I put alert statement at almost every line, and it seemed to pass through all code ok (like it had an status of 0 and a readyState of 4). I think my Id for document.getElementById is wrong, but I'm not sure what else to put. I know in the url I posted that they used targetId, but I don't think I should use it since I don't define it.

Please let me know if anything is unclear. Thank you so much :)

Your PHP code needs to echo the data not return it. Because the function is being called by AJAX, not another PHP function, the call needs to be treated from a PHP standpoint as a page request, not a function call. Make sure something is also calling the function when AJAX invokes that php script.

<?php
function checkjson() {
    foreach (glob("*.json") as $filename) {
        echo $filename;
    }
checkjson();
?>

Your second problem is that Scene.loadObject(jsonstuff); will have already executed before the ajax can alter the page with the response. That function needs to be called after the ajax request completes inside of the following block: (Also the status code for success is 200, not 0.)

if(ajax.status == 200){
   div.innerHTML = ajax.responseText;
}

Edit: Using jQUery will also make your life much easier.

AJAX is asynchronous. The request won't have completed when your alert shows you undefined. You need to use a callback function to wait until your request has completed, before you'll be able to access the server's response.