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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵