weixin_33724059 2014-06-08 16:00 采纳率: 0%
浏览 42

Ajax解析器错误

My following javascript code doesn't working and return the following message : parsererror
resources.php should return the following :{"cpu":"1.12"}
resources.php file contains :

<?php $load = sys_getloadavg(); echo '{"cpu":"'.json_encode($load[0]).'"}'; ?>

Javascript code :

        $.ajax({
        url : 'resources.php', //Target URL for JSON file
        contentType: 'application/json; charset=utf-8',
        type: 'GET',
        dataType: 'json',
        async : false,
        success : function(data){
            console.log(data);
        },
        error : function(xhr, status){
            console.log(status);
            alert(status);
        }
    });
  • 写回答

1条回答 默认 最新

  • lrony* 2014-06-08 16:45
    关注

    I think you missed the header in php. can check it properly

    <?php
        header('Content-type: application/json; charset=utf-8');
        $load = sys_getloadavg();
        $data = array('cpu' => $load);
        echo json_encode($data);
    ?>
    

    jquery:

    $.ajax({
        url : 'resources.php',
        dataType: 'json',
        success : function(data){
            console.log(data);
        },
        error : function(xhr, status){
            console.log(status);
        }
    });
    
    评论

报告相同问题?