How can I get data instantly with ajax?
I am trying to get data instantly with ajax, but I couldn't. The problem is, when I make a request, response is coming end of the php process. I want to get data after every echo command. So here is the simple example. There is two files, main html file (included javascript) and php file.
try.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Get Data</title>
<script type="text/javascript">
function makeObject() {
var newObject;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
newObject = new ActiveXObject("Microso开发者_JAVA百科ft.XMLHTTP");
}
else {
newObject = new XMLHttpRequest();
}
if (newObject.overrideMimeType) {
newObject.overrideMimeType('text/xml; charset=UTF-8;');
}
return newObject;
}
var newOne=makeObject();
function getData()
{
newOne.open('get','process.php',true);
newOne.onreadystatechange=function(){
if (newOne.readyState==4)
{
var box=document.getElementById("queryResult");
box.innerHTML=newOne.responseText;
}
}
newOne.send(null);
}
</script>
</head>
<body>
<input type="button" id="doit" value="Start Query" onclick="getData();" />
<div id="queryResult"></div>
</body>
</html>
and process.php
<?php
echo "1";
sleep(1);
echo "2";
sleep(1);
echo "3";
sleep(1);
echo "4";
sleep(1);
echo "5";
?>
when I click the Start Query button, it is waiting 4 seconds and then write 12345 at the same time. I want to write 1 and wait 1 sec then write 2 and wait 1 sec then write 3 etc. How can I do that? Sorry for my English, thanks for the answers :)
ajax waits for a response, so you are making the final response wait. you would have to do seperate requests. Possibly set subsequent request of dependant upon the result of the previous one(s).
You need to use an html5 web socket. Using standard ajax will not return until the request has completed.
your code is doing exactly what you ask. The ajax response is supplied to you on the client side when the last byte is received not as it is received.
if you are after progress from the server side to appear on the client side then the client must poll and the server maintain a some state to respond, not as you have it with the server trying to send a stream response. The new HTML5 client side sockets may help you here where rather than polling the client can be called by the server
If I understand your question you will need to make 4 ajax method calls. Since you are expecting different values from each call you need to have the state saved somewhere (on client or server) so that you know which call you are on. If the pauses in your sample are part of your intended program (and not just for the sake of the sample) then it would probably be better in your client-side Javascript.
Servers buffer their output - sending a single character is extraordinarly wasteful of network resources. To force PHP and the webserver to flush their buffers, you need at least:
echo "1";
flush();
ob_flush();
echo "2";
etc...
精彩评论