douchuitang0331 2013-06-24 20:48
浏览 43
已采纳

通过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 :)

  • 写回答

2条回答 默认 最新

  • dongqie4233 2013-06-24 20:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?