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

2022-01-04 00:00:00 json get php ajax

我在 jquery 中有一个非常简单的 ajax GET 请求,它在我的本地测试服务器上运行良好.如果我将网页与 php 服务一起移动到托管服务器 (GoDaddy),它会失败并显示 textStatus = "error"errorThrown = "".Chrome 显示

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.

net::ERR_EMPTY_RESPONSE.

这一切都在 1 秒内发生,所以这不是超时问题.

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

如果我截断记录数使返回的 json 小于 1MB,它就可以正常工作.

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

如果我将服务器代码从 GoDaddy 网页调用到我的本地服务器以获取所有记录 (1.8MB),一切都很好.任一站点的良好回报都将在不到一秒的时间内完成.

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.

当 json 回显中有超过 1MB 的数据时,什么可能导致 GoDaddy 基本上不返回数据?

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

php 服务器例程:

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;

javascript 例程:

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);
    });

推荐答案

好吧,我偶然发现了一个解决方案.它是在 echo 之前添加一个 header 语句,它似乎允许 echo 大于 1MB.

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));

我现在需要更多地了解这一点并在各种浏览器中进行检查.同样有趣的是,数据现在作为对象返回,因此无需对其进行 json 解码.

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.

我怀疑 GoDaddy 服务中的某些内容默认了长度,但我不确定.再次,我的服务器没有这样做.

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.

相关文章