dtp791357 2012-11-05 18:22
浏览 294

调试失败ajax POST调用,类型处于挂起状态

I have a javascript file that generates some JSON and makes an AJAX call to a php file that writes and creates the JSON file. Everything has been working well until I started generating pretty big JSON files. Currently, I have a 9mb json file and I know it is structured correctly because when I print it out using console.log and manually put into a file, it works.

However, the Ajax call fails to succeed. I have tried a normal XHR call and jQuery Ajax, both fail and do not provide much information. I tried in Chrome and Firefox. Looking at Developer Tools, the status text says (failed) and type says pending. No status code appears and all I have is statusText: "error" in Ajax error callback. I tried messing around with timeout and synchronous calls but nothing seems to work/

Any ideas on how I can debug further?

Code Sample

var name = "test.json";
var json = '{
    "type": "FeatureCollection",
    "bbox": [
        1.4137153625488281,
        42.4288330078125,
        1.7842082977294922,
        42.652313232421875
    ],
    "features": [
        {
            "type": "Feature",
            "bbox": [
                1.4137153625488281,
                42.4288330078125,
                1.7842082977294922,
                42.652313232421875
            ],
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            1.5753068923950195,
                            42.6473388671875
                        ],
                        [
                            1.5828666687011719,
                            42.63543701171875
                        ],
                        [
                            1.5753068923950195,
                            42.6473388671875
                        ]
                    ]
                ]
            },
            "properties": {
                "ID_0": "6",
                "ISO": "AND",
                "NAME_ENGLI": "Andorra",
                "NAME_OBSOL": ""
            }
        }
    ]
}';



$.ajax({
        type:"POST",
        url:"log.php",
        data: { data: json, file: name },
        dataType: "json",
        success:function(response) {
            console.log(response);
        },
        error:function(error) {
            console.log(error);
        }
});

This is log.php:

if (isset($_POST['file'])) {
$file = $_POST['file'];
$data = str_replace("\\", "", $_POST['data']);

file_put_contents($file, $data);
exit();
}

My main question is this, when AJAX fails, how do you debug better? My error callback here does not provide any good information. I tried looking at net-internals in chrome but I can't find much there. Any ideas?

Perhaps this is a Server Side problem. When I look at Chrome net-internals I see that processing a smaller JSON (length = 121664) makes a successful POST request and net-internals show HTTP_TRANSACTION_READ_RESPONSE_HEADERS but the large JSON (length = 7036490) give me --> net_error = -324 (ERR_EMPTY_RESPONSE) instead of response headers. I did not see a timeout information in the phpinfo though...

  • 写回答

1条回答 默认 最新

  • dongnuo6310 2012-11-05 20:26
    关注

    First, check your error logs on the server side.
    Try increasing maxRequestLength setting in PHP, if the request takes to long to handle.

    If it's a client side problem your best bet to get it to work cross browser would be to split your post on the client side and posting it in several parts. On the server side you could save it in a temporary file for instance.

    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容