...

View Full Version : Problem with ajax



dark0s
09-03-2010, 03:08 PM
I have the following ajax code:



.............
.............
.............
XHR.createXMLHttp();
XHR.open("POST", "/cgi-bin/addpost.cgi", true);
XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
XHR.onreadystatechange = function() {
if (XHR.readyState == 4) {
if (XHR.status == 200) {
alert("Operazione effettuata con successo");
}
else {
alert("Operazione non riuscita");
}
}
}
XHR.send(url);


The function send data, but it don't return never alert windows. Where is the problem?

Old Pedant
09-03-2010, 07:36 PM
Who knows? You don't show what that XHR object looks like.

You don't show what the url variable contains.

Maybe the AJAX call isn't working at all.

dark0s
09-04-2010, 08:49 AM
The full code is:



function createXMLHttp() {

if (typeof XMLHttpRequest != 'undefined') {
return new XMLHttpRequest();
}
else {
throw new Error('XMLHttp object could not be created!');
}

}

function sendPost() {

var title = document.getElementById('title').value;
var post = document.getElementById('post').value;
var profile = document.getElementById('profile').value;
var datetime = document.getElementById('datetime').value;
var url = "?title=" + title + "&post=" + post + "&profile=" + profile +"&datetime=" + datetime;
var XHR = null;

XHR.createXMLHttp();
XHR.open("POST", "/cgi-bin/addpost.cgi", true);
XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
XHR.onreadystatechange = function() {
if (XHR.readyState == 4) {
if (XHR.status == 200) {
alert("Operazione effettuata con successo");
}
else {
alert("Operazione non riuscita");
}
}
}
XHR.send(url);

return false;

}


Where is the problem?

gusblake
09-04-2010, 11:19 AM
XHR.createXMLHttp is undefined. I think it should be XHR=createXMLHttp(); (line 21)

dark0s
09-04-2010, 11:40 AM
Ok, now it works.
I have other two questions:
1) I modified sendPost() function, it works but return "NOT OK" message with alert, where is the problem?


function sendPost() {

var title = document.getElementById('title').value;
var post = document.getElementById('post').value;
var profile = document.getElementById('profile').value;
var datetime = document.getElementById('datetime').value;
var url = "?title=" + title + "&post=" + post + "&profile=" + profile +"&datetime=" + datetime;
var XHR = null;

XHR = createXMLHttp();
XHR.open("POST", "/cgi-bin/addpost.cgi", true);
XHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
XHR.onreadystatechange = function() {
if (XHR.readyState == 4) {
if (XHR.status == 200 || XHR.status == 304) {
alert("OK");
}
else {
alert("NOT OK");
}
}
}
XHR.send(url);

}


XHR.status is 0

2) what I return server side? I must build all web page or only a piece of hit?

gusblake
09-04-2010, 10:18 PM
Not sure about 1. Maybe try it on a few different pages/sites to determine whether it's server- or client side.

2: You would usually return a string of data or a snippet of HTML to be added to the current DOM; it works like this:



if (XHR.status == 200 || XHR.status == 304) {
//either:
var data=XHR.responseText; //data contains whatever string the server sent back

//or:
var xml=XHR.responseXML; //xml is an XML document object which can be manipulated with standard DOM methods

alert("OK");
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum