Reputation: 174
I've been trying to get this long polling script to work but I keep getting stuck. This time I have a problem with the ajax request to the server. The php script that serves the data (running the long-poll using a loop and sleep) works perfectly fine but my jQuery script doesn't.
So far I've tried using a normal $.ajax()
request and a $.post()
request to the script however when I do so the entire script stalls. Well the script actually loads like it is supposed to but the page shows that it keeps loading (using google chrome it shows the "waiting for" bar at the bottom of the screen). After the 60 second timeout in the php script the jQuery script receives "false" because no data was found. The script is then supposed to ask for the data agian.
My question is: how do I avoid the browser showing that it's loading something in the background? My server is a shared server so I don't have access when it comes to installing stuff on the server itself so this have to be solved using only javascript (jQuery) and php.
EDIT: just checked my script out in Internet Explorer and it doesn't seem to be showing any signs that the script is loading but it's still a problem in Google Chrome
EDIT Posting some code:
This is my javascript file:
if($("#interestboard"))
{
var lasttime = $("#lasttime").val();
var pollurl = $("#pollurl").val();
lpStart();
}
function lpStart()
{
$.ajax({
type: "GET",
url: pollurl+lasttime,
async: true,
cache: false,
success:function(data)
{
alert(data);
},
error: function()
{
alert("Something went wrong");
}
});
}
Of course this is wrapped within the $(document).ready()
My php script is something like the following:
while(time()-$time<60)
{
$this->class = $class["id"];
$this->orderBy("timestamp");
$this->_extraConditions = "`timestamp`>'$lastTime' AND ";
$result = $this->search();
if($result)
return $result;
usleep(250000);
clearstatcache();
}
When using firebug (with Google Chrome) it only shows that a request has been made but not showing the usual loading-icon at the end of the GET string in the console. In Firefox it also just shows that a request is pending (showing the loading icon) but at least the browser doesn't act like the page is loading.
Thanks...
Upvotes: 3
Views: 5912
Reputation: 2502
I think you should read top comment on Chrome AJAX on page-load causes "busy cursor" to remain - it seems it is Google Chrome specific and you need to wait before ALL content is loaded before making an ajax call.
EDIT: Just a recommendation - maby I am wrong: you should not use GET requests to make any huge/sensitive operations on your site as it can lead to and easy attack on your site. Anyone who owns/hacks some high-load site can insert an invisible iframe to their page so that any user that visits that page will make that GET request without even realising it - it can crash/harm your server (as thousands requests comes to your server). Most modern browsers will not let cross-site POST requests, so it should be safe.
EDIT 2: About your site being unavaliable while that script is still working even after you abort it: it is most likely because of sessions. Standard session behaviour will let only one php instance with same session running at the same moment - all other requests will wait untill that session is closed in all other scripts. You should close the session ASAP in script, use custom session handling that will enable multiple requests with the same session(most likely this will be really ugly) or detect user abort to close the sript as soon as user aborted it in browser.
Upvotes: 3