I’m having a bit of trouble getting this Ajax post request to work properly.
The whole js file contains my GET function, which works fine, but I can’t seem to get the post one working. It hangs on the line I’ve highlighted when I call it:
var xmlhttp=false;
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}
var please_wait = "Loading...";
function open_url(oldurl, targetId) {
if(!xmlhttp)return false;
var e=document.getElementById(targetId);if(!e)return false;
if(please_wait)e.innerHTML = please_wait;
urlsize = oldurl.length-3;
if (oldurl.indexOf('php') == urlsize)
{
url = oldurl + '?rannum=' + Math.floor(Math.random()*100001);
} else {
url = oldurl + '&rannum=' + Math.floor(Math.random()*100001);
}
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange = function() { response(url, e); }
try{
xmlhttp.send(null);
}catch(l){
while(e.firstChild)e.removeChild(e.firstChild);//e.innerHTML="" the standard way
e.appendChild(document.createTextNode("request failed"));
}
}
function post_reply(url, targetId) {
if(!xmlhttp)return false;
var e=document.getElementById(targetId);if(!e)return false;
if(please_wait)e.innerHTML = please_wait;
urlsize = oldurl.length-3;
//params = "pid=2&title=ThisIsIt";
xmlhttp.open("POST", url, true);
[COLOR="Red"] xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");[/COLOR]
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.onreadystatechange = function() { response(url, e); }
try{
xmlhttp.send(params);
}catch(l){
while(e.firstChild)e.removeChild(e.firstChild);//e.innerHTML="" the standard way
e.appendChild(document.createTextNode("request failed"));
}
}
function response(url, e) {
if(xmlhttp.readyState != 4)return;
var tmp= (xmlhttp.status == 200 || xmlhttp.status == 0) ? xmlhttp.responseText : "<h2><font color=\\"red\\">Error: \\"" + xmlhttp.status+" "+xmlhttp.statusText + "\\"</font></h2><h3>Please let a member of admin know that the link/function you used doesn't work.</h3><h3><a href=\\"javascript:void(0);\\" onclick=\\"open_url('pages/pages.php','main');\\">Homepage</a></h3>";
var d=document.createElement("div");
d.innerHTML=tmp;
setTimeout(function(){
while(e.firstChild)e.removeChild(e.firstChild);//e.innerHTML="" the standard way
e.appendChild(d);
},10)
}
As you can see from the code, when I call something like:
open_url('pages/home.php','main');
it works fine, but the post_reply() function just hangs.
I’m getting quite frustrated with this. Can anyone give it an eyeball check to see if I’ve done something wrong or missed something please?
When params is defined, I get the same problem, plus it actually get’s stopped on the line above params.length (I put in alert()'s either side and only one showed). I //'d the params line to see if it would fix the problem, but it didn’t, I just forgot to take out the // before I posted.
Now the problem is obvious - you’re initialising an asychronous request but doing nothing to prevent the form being submitted. Consequently the document is dismissed before the request can complete.
If you’re retrieving data for display or just displaying the result of the submission, you shouldn’t be trying to submit a form.
I don’t think that’s it either. I changed my form so it doesn’t submit anymore, and put the javascript call in the onclick function on the button like so:
Same problem. When I change post_reply(‘pages/process.php’,‘main’); to open_url(‘pages/process.php’,‘main’); process.php loads. So the problem must be with the ajax POST code somewhere.
I’m still scratching my head
David
EDIT:
I fixed it!!!
I left this line in from when I copied the text from the GET command:
urlsize = oldurl.length-3;
Of course oldurl doesn’t exists, so it was stopping!
Yay, I’m happy now. Thanks for all your help Logic Ali!