duanmei9980 2013-05-01 19:34
浏览 52
已采纳

php文件I / O和ajax的问题

I'm trying to polling with ajax and php for an app Im writing. The php essentially receives an stringified object to put into a file, that can be read later. I have the writing to objects working just fine. The problem I have right now is that if the php tries to read a file that doesn't exist, it seems to return an error code to the $.post() call. I want it to return JSON, and success, so that I can process is a bit differently.

The php in question is as follows:

public function read(){
        $id = $this->input->post('id');
        $test = fopen("./meetings/".$id.".mt", 'r');
        if(!$test){
            error_log("Unable to open file, you mutt!");
            echo json_encode(array('status' => "FAIL"));
        }else{
        error_log("Here! 2");
        $obj = array();
            while(!feof($test)){
                error_log("Here! 3");
                $tmp = fgets($test);
                error_log("Here! 4");
                if($tmp){
                    $obj[count($obj)] = $tmp;
                }
            }
            fclose($test);
            error_log("Here! 5");
        if(!count($obj) > 0){
            echo json_encode(array('status' => "FAIL"));
        }
        echo json_encode(array('status' => 'OKAY', 'obj' => $obj));
        }
    }

and the Javascript(using jquery) is as follows:

function read(){
        $.post('<?php echo base_url(); ?>meetings/read', {id: <?php echo $id;?>}, function(json){
            console.log(json.status);
            if(json.status == 'OKAY'){
                for(var i = 0 ; i < json.obj.length ; i++){
                    parseObject(json.obj[i]);
                }
            }
        }, 'json');
    }

the javascript doesn't get to the success function, I checked using ajaxError() but I'm not sure how to fix this, or even why the php is sending an error cause I think I'm ckecking for it. The php works as expected otherwise. also, the polling is indeed calling the server as frequently as it should be. Any suggestions on fixing this? If you need any more information please just ask. Thanks!

  • 写回答

1条回答 默认 最新

  • douhaodang0403 2013-05-01 20:36
    关注
    public function read(){
       $id = $this->input->post('id');
    
       /* changes */
       $path = "./meetings/".$id.".mt";
       if (!file_exists($path)){
         echo json_encode(array('status' => "FAIL")); 
         return;// this exit from here and go to js file
       }
       /* changes ends */
    
        $test = fopen($path, 'r');
        if(!$test){
            error_log("Unable to open file, you mutt!");
            echo json_encode(array('status' => "FAIL"));
        }else{
        error_log("Here! 2");
        $obj = array();
            while(!feof($test)){
                error_log("Here! 3");
                $tmp = fgets($test);
                error_log("Here! 4");
                if($tmp){
                    $obj[count($obj)] = $tmp;
                }
            }
            fclose($test);
            error_log("Here! 5");
        if(!count($obj) > 0){
            echo json_encode(array('status' => "FAIL"));
        }
        echo json_encode(array('status' => 'OKAY', 'obj' => $obj));
        }
    }
    

    I tried your code using fixed constant and it worked. If still facing error wrap 'fopen' in try-catch block.In other case check path settings. Use firebug to see actual JS generated.use that if stucked.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等