I looked at a brief introduction to ajax long polling here and tried to mimic it on my own pc using wamp, however I have encountered a couple of problems.
I kept getting an error about undefined index in the longpolling.php file, line 29 which is
$num = $_GET['num'];This was because there was one ajax get function that didn't have a num parameter resulting in $_GET not being set. I changed the code to
if(isset($_GET['num'])) $num = $_GET['num']; else $num = "";
and that works fine. However, once i reload the page, the cd count decrements once and then stops.
Would anyone know the reason why this is happening? php file (server)
<?php
$cd_stock = ("CdCount.txt");
function updateStock($num)
{
global $cd_stock;
$count = file($cd_stock);
$count = (int)$count[0];
$count = $count - $num;
if ($count < 0) $count = 0;
$fp = fopen($cd_stock , "w");
fputs($fp , "$count");
fclose($fp);
echo $count;
}
function getCdCount()
{
srand();
$newOrder = rand(1, 3);
$sleeptime = rand(2, 10);
sleep(2);
updateStock($newOrder);
}
if(isset($_GET['num']))
$num = $_GET['num'];
else
$num =开发者_JS百科 "";
if ( $num = "")
{
getCdCount();
}
else
{
updateStock((int)$num);
}
?>
javascript file (client) (uses prototype framework)
Event.observe(window, 'load', function() {
Event.observe( 'btnSubmit', 'click', purchaseCD);
connectToServer();
});
function connectToServer()
{
new Ajax.Updater(
{ success: 'CD Count', failure: 'errors' },
'LongPolling.php',
{
method: 'get',
onSuccess: function(transport)
{
if (parseInt(transport.responseText)) connectToServer();
}
});
}
function purchaseCD()
{
new Ajax.Updater(
{ success: 'CD Count', failure: 'errors' },
'LongPolling.php',
{
method: 'get',
parameters: { num: $('txtQty').getValue() }
});
}
The html file isn't really worth posting up. it just includes the ajax javascript file and the prototype js file and the relevant divs etc.
I've been racking my brain for hours trying to solve this but I have no idea what is wrong, and it is not encouraging that this is coming from a "tutorial" type article.
The very fact that your first problem could exist is a tutorial is, frankly, unforgivable. All tutorials should work with the assumption that you have maximum error reporting on (and it would be great if all of them told you how to ensure that it happened). The good news for you? WAMP sets error reporting pretty high by default, so there should be no worries there.
I also take issue with the fact that he is using file
that way (he should be using file_get_contents
) and that he is telling you to use a file to begin with -- it should be $_SESSION
or, even better, a database connection. A tutorial should also avoid the use of the keyword global
by all means necessary (and if a tutorial can't manage that, then maybe the author should be going through tutorials instead of writing them). He also thinks it a good idea for you to be using an ID which is invalid CSS (you can't have spaces in your ID's in CSS. It is acceptable in JS, but why would you do it if it makes CSS impossible). Finally, he talks about all of the benefit of Ajax.Updater, but he decides to use a bizarro recursion scheme instead of Ajax.PeriodicalUpdater. That's what it's there for.
My recommendation is to ditch that tutorial and work on a better one. Personally, I find value in learning "the old ways" (no-framework AJAX), I have to recommend this one. If you want to use a framework, personally I think you are better off with a jQuery tutorial (like this one) as it is far more common. If you really like Prototype, there are other, simpler examples -- this one seems very strait forward.
If you feel that this is the one, true tutorial, above all else, then there are some things you can do to help yourself sort this out. You don't have any glaring coding mistakes, so the most I can do is give you advise:
- Make sure WAMP is still running (stupid step, I know, but it happens to quite a few of us on occasion).
- Replace "sleep" in PHP with
if (parseInt(transport.responseText)) setTimeout(connectToServer, Math.rand() * 4000 + 1000);
(places the delay client-side instead of server-side).sleep
should realistically only be used in extraordinarily rare circumstances. - Call
console.log
(or alert if you're not using a web developer tool like Firebug (and if not... why not?)) ontransport.responseText
. If the server is returning 0, it won't fire the connectToServer method again. So knowing what that value is might be useful. - Track the Ajax requests. You can do this with Firebug for free.
- Look at CdCount.txt, see if it is updating at all.
精彩评论