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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料