dsyrdwdbo47282676 2018-09-29 22:05
浏览 101
已采纳

当GoDaddy主机上的JSON内容超过1MB时,ajax GET失败

I have a very simple ajax GET request in jquery that works just fine on my local test server. If I move the web page to a hosted server (GoDaddy) along with the php service it fails with a textStatus = "error" and errorThrown = "". Chrome displays

net::ERR_EMPTY_RESPONSE.

This all happens within 1 second so it's not a timeout issue.

If I truncate the number of records so the json returned is less than 1MB it works fine.

If I call the server code from GoDaddy webpage to my local server for all records (1.8MB) all is fine too. The good returns from either site will complete in less than a second.

What could be causing GoDaddy to basically return no data when the json echo has more than 1MB of data in it?

The php server routine:

if (isset($_REQUEST['_SESSION'])) die("Get lost Dweeb!");
error_reporting(E_ALL | E_STRICT);
header('Access-Control-Allow-Origin: *');
$date_code = $_GET['date_code'];
$region = $_GET['region'];
$chargers = array();
$chg_count = 0;
$ftime = filemtime("chargers.json");

if ($ftime != $date_code) {
    $aTeslaChargers = json_decode(file_get_contents("chargers.json"),true);
    foreach($aTeslaChargers as $aTeslaCharger) {
        if ($aTeslaCharger['region'] == $region) {
            $chargers[] = $aTeslaCharger;
            $chg_count++;
            //if ($chg_count > 1972) break;
        }
    }
}
$json = json_encode(array(array("date_code" => $ftime), $chargers));
echo $json;

The javascript routine:

var url = 'https://www.website.com/get_data.php?date_code=0&region=north_america'; 

    var jqxhr = $.ajax({
                url: url,
                type: "GET",
                crossDomain: true
    })
    .done(function(response) {
        console.log(new Date());
        //var data = $.parseJSON(response);
        //console.log(data);
        console.log(response.length);
    })
    .fail(function(jqXHR, textStatus, errorThrown) {
        console.log(new Date());
        console.log(errorThrown);
    });
  • 写回答

1条回答 默认 最新

  • doubo3384 2018-09-30 23:27
    关注

    Well I stumbled onto a solution. It was to add a header statement just before the echo which seems to allow the echo to be larger than 1MB.

    header("Content-Type: application/json");
    echo json_encode(array(array("date_code" => $ftime), $chargers));
    

    I now need to understand this more and check it across various browsers. Also interesting that the data was returned as an object now so no need to do json decode on it.

    I suspect there is something in the GoDaddy service that defaulted the length but I am not sure. Again my server did not behave this way.

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

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制