当GoDaddy主机上的JSON内容超过1MB时ajax GET失败 [英] ajax GET failing when JSON content over 1MB on GoDaddy host

查看:103
本文介绍了当GoDaddy主机上的JSON内容超过1MB时ajax GET失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

好吧,我偶然发现了一个解决方案.只是在回显之前添加了一个头声明,这似乎允许回显大于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.

这篇关于当GoDaddy主机上的JSON内容超过1MB时ajax GET失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆