admin管理员组文章数量:1291797
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 puter, 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:
- I tried Xampp but it's not patible with our framework
- I tried going to Apache 2.0 instead of 2.2 in wamp
- I tried with different configurations of the httpd.conf and php.ini
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.
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 puter, 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:
- I tried Xampp but it's not patible with our framework
- I tried going to Apache 2.0 instead of 2.2 in wamp
- I tried with different configurations of the httpd.conf and php.ini
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.
Share Improve this question edited Mar 18, 2023 at 23:31 halfer 20.5k19 gold badges109 silver badges202 bronze badges asked Feb 5, 2009 at 20:35 DFectuosoDFectuoso 4,89713 gold badges41 silver badges56 bronze badges 2- Have you tried using a normal HTML form to post directly, instead of Javascript? Which methods below have you tried? It would be great if you could provide such information so we can eliminate possible issues. – strager Commented Feb 15, 2009 at 22:30
- To note, I have installed WAMP (latest) and attempted to reproduce the bug but failed. After around 100 clicks I never once got a bad response. – strager Commented Feb 15, 2009 at 22:31
12 Answers
Reset to default 2I'd suggest installing WireShark and monitoring your HTTP traffic to see if you can isolate the problem that way.
Have you tried another version of Prototype? It seems to me, there's a weird bug in constructing the post request body effectively creating invalid (partly) request the server cannot properly parse. Use Fiddler (easy to grasp http sniffer) and see what exactly is being sent to the server.
If this is not the case (I admit it would be really weird if Prototype would be broken), try reading raw post data via the PHP (should be enabled in php.ini). If you can and they are not populated into the $_POST
collection, try $_REQUEST
instead.
Also, try to consult the following thread, maybe this is your case: http://bugs.php/bug.php?id=41349 .
This is a long shot, but without more information and seeing more of your code I have to ask this so you could rule it out: Could it have something to do with the user double-clicking on the link and creating two rapid requests? (Once upon a time I got bitten by <input type="image" onclick="submit();"> both run the onclick and submitted the form)
function test() {
voteAjax('content','up','89');
voteAjax('content','up','89');
}
(have your link call the test function above)
Normally you want to use asynchronous connections since that won't lock the UI, however this is a case where you could try synchronous to make sure you only do one request at the time. (a nonblocking solution is to set a flag or disable the link/button while the request is processed)
BTW, I too suggest that you send an object instead of a string as the parameters, since prototype will URIEncode it and put all & at the right places.
I also suggest you should monitor your network traffic through Wireshark:
- Start it on either server or client.
- Start capturing your network interface.
- Use the AJAX and
- when it fails stop the capture and
- type http in Wireshark to help you find the request.
- 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 remend switching to PHP module or look for possible configuration mistakes.
What WAMP is it, anyway? Pre-configured or custom?
Try new Date(), instead of random:
voteAjax = function(typez, actionz, idz){
new Ajax.Request(
'http://localhost/reporeade/Vote/Ajax/',
{asynchronous:true,
evalScripts:true,
method:'post',
parameters:'contentType='+typez+'&action='+actionz+'&id='+idz+'&now='+new Date()
});
//alert("params" + typez.toString() + actionz.toString() + idz.toString());
return false;
}
try this:
voteAjax = function(typez, actionz, idz){
new Ajax.Request(
'http://localhost/reporeade/Vote/Ajax/',
{
parameters: {
contentType: typez,
action: actionz,
id: idz,
rand: new Date()
}
});
return false;
}
notice that I've removed the other options since they aren't necesary, due to they are on it's default values :)
asynchronous:true,
evalScripts:true, -----> this one is "evalJS" not "evalScripts" :)
method:'post',
yo can see it here http://www.prototypejs/api/ajax/options
hope its helps!
I'm running wampserver, too, so I just tried your code, and I could not reproduce the problem -- I always get the POST values. My install is Apache 2.2.8 and PHP 5.2.6, and I just grabbed the latest prototype.js. My wampserver configuration is pretty much the default.
+1 to Fiddler and/or Firebug. Looking at the raw headers that are being sent will probably be enlightening!
What browser are you using? This fails when using FF and IE? Or only one browser.
Sometime ago I read about IE and Apache gzip module problems. It was occurring only on local installations where a server response was very fast. However your case is different.
It is definitely worth trying WireShark as others already suggested. It isn't that hard. At least you'll known on which side there is a bug.
If its a POST request you don't need to set the rand parameter, POST isn't cached, maybe that helps.
EDIT Can't really understand what's causing this without seeing more code in detail. What I would do is:
1) Try passing an object to the parameters
attribute like this:
parameters: {
contentType, typez,
action: actionz,
id: idz
}
instead of a string:
parameters:'contentType='+typez+'&action='+actionz+'&id='+idz
2) Why do you need evalScripts
? For purpose of testing remove it.
3) Debug the hell out of this, seems like the problem is with the parameters being sent with Javascript, so use Firebug's console.log()
to debug this more in depth.
Maybe some ponent where the request passes through (Php, webserver, firewall, ...) gets confused that you use a POST request with GET parameters. Does it change anything if you leave out the ?rand=...
part?
What happens if you use a GET instead. I mean, if you concatenate the other parameters in the URL too? Like this
voteAjax = function(typez, actionz, idz){
new Ajax.Request(
'http://localhost/reporeade/Vote/Ajax?'+'contentType='+typez+'&action='+actionz+'&id='+idz+'&now='+new Date(),
{asynchronous:true,
evalScripts:true,
method:'post',
parameters:''
});
return false;
}
I think you can use GET.
Hope this help.
I'd do the Ajax call like this:
voteAjax = function(typez, actionz, idz){
var rndVal = Math.random()*500000;
new Ajax.Request( 'http://localhost/reporeade/Vote/Ajax/', {
method: 'post',
parameters: {contentType: typez, action: actionz, id: idz, rand: rndVal},
onSuccess: function(response) {
//do stuff
},
onFailure: function() {
alert('Something went wrong.');
}
});
return false;
}
本文标签: phpWhat is the problem with this ajax (with prototype)Stack Overflow
版权声明:本文标题:php - What is the problem with this ajax (with prototype)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741540223a2384279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论