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条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况