Ajax behaving differently on Firefox

你离开我真会死。 提交于 2019-12-08 13:16:54

问题


I am basically trying to check the progress of a file download. To do this, I am repeatedly polling a php file(progress_sess.php) which is echoing the session value being changed in the file handling the download (export.php). So there is one ajax request to export.php and multiple ajax requests to progress_sess.php. This way I am able to display the phase that the file download script is currently in. All is working out well in chromium. which is clear from the requests, you can see the image here: - Chromium Requests.

In firefox However this error is thrown: -

JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

You can see the request here: - Firefox Requests When I echo the xhr.responseText in Firefox, it gives this: -

""

This is possibly because in Chromium as you can see in the image, the requests are application/json, as specified in the progress_sess.php header, whereas in firefox the first request is for some inexplicable reason text/plain?! So i do not understand why firefox is getting a text/plain response header.

You can see progress_sess.php: -

<?php
    session_id($_COOKIE['phpMyAdmin']);
    session_start();
    header("Content-Type: application/json");
    $result = $_SESSION['export_progress'];
    if ($result==null) 
    {
        $result = '';
    }
    $arr = array(progress_result=>$result, source=>'progress_sess.php');
    echo json_encode($arr);
?>

So basically it is impossible that there is something wrong with my json. So i guess my question is, why is it throwing json parse error when I have explicitly specified a header? is it because of the text/plain request header, or due to some ajax differences between firefox and chromium.


回答1:


Seems like your response is not a valid json-string. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse



来源:https://stackoverflow.com/questions/29088420/ajax-behaving-differently-on-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!