weixin_33725807 2015-10-12 01:25 采纳率: 0%
浏览 37

为什么服务器在运行js后总是关闭?

我希望每15秒从另一个服务器动态地获取数据XML,然后将XML数据转换为json,并将其保存到我的服务器上的一个新文件中。

我的jQuery代码:

(function($){
    function getData() {
        $.ajax({
            url: 'data.php',
            type: 'GET',
            dataType: 'json',
            beforeSend: function () {},
            success: function (response) {
                console.log(response);
            }
        });
    }

    var refreshData = setInterval(getData, 15000);

    getData();
})(jQuery);

data.php文件中的代码:

// load class
<?php
require_once 'class/rii.class.php';

// get data
$data = new RII_Data( 'http://theotherservertoget/pri2.xml' );

// generate json
$data->generate_json();

rii.class.php文件中的代码:

<?php
class RII_Data {
    private $options = array();

    function __construct(  $url = '' ) {
        $this->options['url'] = $url;
    }

    private function get_json( $url ) {
        $xml_string = @file_get_contents( $url );
        $xml_string = str_replace( array( "
", "
", "\t"), '', $xml_string );
        $xml_string = trim (str_replace( '"', "'", $xml_string ) );
        $xml = simplexml_load_string( $xml_string );
        $json = json_encode( $xml );
        return $json;
    }

    private function print_json( $json ) {
        header('Content-Type: application/json');
        echo $json;
        exit;
    }

    public function generate_json() {
        $json = $this->get_json( $this->options['url'] );
        $this->print_json( $json );
    }
}

我的服务器在运行js后总是关闭,是哪里出错误了吗?

  • 写回答

1条回答 默认 最新

  • weixin_33736832 2015-10-12 01:52
    关注
    var refreshData = setInterval(getData, 15000);
    

    You are using eager loading method to refresh your page every 15 secs. I think that's the problem causing your web server to consume lot of resource.
    You should just update what has been changed instead of re-convert all the xml to json.
    Edited: The simplexml convert whole xml tree into objects which will consume a lot of resource. If you can get rid of it, using string manipulating function to get the job done, it will be better.
    Regards,

    评论

报告相同问题?