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 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持