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 github符合条件20分钟秒到账,github空投 提供github账号可兑换💰感兴趣的可以找我交流一下
  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?