phpjavascriptajaxprototypejs

What is the problem with this ajax (with prototype)?


I have the following code.

PHP side:

<?php
print_r($_POST);
print_r($_GET);
die();
?>

In JavaScript:

voteAjax = function(typez, actionz, idz){
    new Ajax.Request(
        'http://localhost/reporeade/Vote/Ajax/?rand='+Math.random()*500000, 
        {asynchronous:true, 
         evalScripts:true,
         method:'post',
         parameters:'contentType='+typez+'&action='+actionz+'&id='+idz 
        });
    return false; 
}

And in some part of my html:

<a class="button" onclick="voteAjax('content','up','89');">

You can see I'm running on localhost... the problem I have is that the POST somehow gets mixed up SOMETIMES, I get the next answer 90% of the time:

Array
(
    [contentType] => content
    [action] => up
    [id] => 89
)

Array
(
    [rand] => 449701.9597706424
)

And the other 10% of the time I get:

Array
(
)

Array
(
    [rand] => 468905.44804602925
)

Now, I have tried everything, changed computer, tried on a server with a full url (thinking maybe localhost was the trouble), read somewhere that using http://localhost/reporeade/Vote/Ajax/?rand= instead of http://localhost/reporeade/Vote/Ajax?rand= solved the trouble but tried both and really cant understand what would make the post get lost.

Edit

After playing with all this, I got it working (like it is) in our production server, but in all of our wamp installations it will not work (well fail like 50% of the time). It's really important for me to solve this so we can keep on developing all the ajax functionality of the product, so:

Why would Wamp fail like this?

Update

I'm sure the problem is the Wamp installation that is not sending the POST correctly some times.


Solution

  • I also suggest you should monitor your network traffic through Wireshark:

    1. Start it on either server or client.
    2. Start capturing your network interface.
    3. Use the AJAX and
    4. when it fails stop the capture and
    5. type http in Wireshark to help you find the request.
    6. Right-click and select "Follow TCP Stream".

    If POST data in the request does not show up it's something with the browser. You could check HTTP headers for possible errors (like GZip in conjunction with IE has been mentioned before).

    Maybe your WAMP setup uses Fast-CGI or similar to call PHP, so it is not using the actual PHP module for Apache? Sometimes this can cause such errors if not set up correctly. If you do not necessarily need it I recommend switching to PHP module or look for possible configuration mistakes.

    What WAMP is it, anyway? Pre-configured or custom?