Ajax没有收到数据

时间:2016-06-28 19:34:20

标签: php jquery ajax apache

我正在尝试将php数据流式传输到ajax处理程序并使用下面的代码在页面上显示它,但在某些设备上,页面回显的结果不会显示。我已经使用wireshark确认主机正在接收数据(可以在tcp数据包中看到它),但它似乎没有成为ajax?什么可能出错?

客户方:

$.ajax( 
    {
        xhrFields: {
            onprogress: function(e)
            {
                var this_response, response = e.currentTarget.response;

                if(last_response_len === false)
                {
                        this_response = response;
                        last_response_len = response.length;
                }
                else
                {
                        this_response = response.substring(last_response_len);
                        last_response_len = response.length;
                }

                /* Put output on page */
                $('#tool-output-area').append(this_response + '<br>');

            }
        },
  method: 'POST',
    url: '../../../../../../../../page.php',
    data:
  { 
    data-key: data-variable...
  }
    })
    .done(function(data)
    {
      // stuff
    })
    .fail(function()
    {
        // stuff
    });
});

服务器端:

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

/* Set Maximum Execution Time By Plan */
ini_set('max_execution_time', getMaxExeTime());

ini_set('output_buffering', 'off');
ini_set('zlib.output_compression', false);
ini_set('implicit_flush', true);

ob_implicit_flush(true);

while (ob_get_level() > 0) 
{
  $level = ob_get_level();
  ob_end_clean();
  if (ob_get_level() == $level) break;
}

if (function_exists('apache_setenv')) 
{
  apache_setenv('no-gzip', '1');
  apache_setenv('dont-vary', '1');
}

// Checks

$descriptorspec = array(
    0 => array("pipe", "r"), 
    1 => array("pipe", "w"), 
    2 => array("file", "proc.txt", "a")
);

$pipes = array();
$process = proc_open('exec ' . escapeshellcmd($tool_command), $descriptorspec, $pipes, '/path-here', null);
$proc_details = proc_get_status($process);
$pid = $proc_details['pid'];

$proc_row = logProcess($pid, $tool_command, $output_file, $tool);

$str = '';

session_write_close();

if(is_resource($process)) 
{
    while ($curStr = fgets($pipes[1])) 
    {
        echo nl2br(htmlentities($curStr)); // Send the data back
        flush();
        $str .= nl2br(htmlentities($curStr));
        $arr = proc_get_status($process);
        sleep(1);
    }
}

fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);

0 个答案:

没有答案