XMLHttpRequest
さまざまな種類の readyState 値を確認するためにテストするコードを作成しようとしています。MDNによると、5 つの連続した状態が考えられます。各状態の間に一時停止を強制する小さな PHP コードを作成しました。
<?php
// Build the json
$msg = json_encode(array(
'status' => 0,
'msg' => 'Lorem ipsum dillum sit amet')
);
// Divide the message in two parts
$msgLen = strlen($msg);
$h1 = $msgLen / 2;
$h2 = $msgLen - $h1;
// Wait, then send the headers
sleep(1);
header('Content-type: application/json');
flush();
// Wait, the send the first part
sleep(1);
echo substr($msg, 0, $h1);
flush();
// Wait, the send the second part
sleep(1);
echo substr($msg, $h2);
問題は、それが機能していないように見えることです。要求は、各状態間で一時停止するのではなく、1 - OPENED から 4 - DONE に即座に進みます。
また、2 つの興味深いことが起こります。
- Firefox と IE では、readyState 1 - OPENED が 2 回発生します。
- IE10 では、ヘッダーが送信されるときに確かに一時停止がありますが、コンテンツの 2 つの部分の間に一時停止はありません。