Hi, I’m new to ajax and I hope someone could help me out here. It’s very frustrating that I cannot see anything wrong with my code and still it doesn’t work 😡 . I just updated the source code a little bit from mozilla site so that instead of having everything in one place I can break it down to small functions.The link for that is here [url]http://developer.mozilla.org/en/docs/AJAX:Getting_Started
For some reason the “postData(…) function is not executing. I couldn’t find what the problem is. If anyone could show me the mistake, that’ll be really helpful. Thanks a lot and Here’s my code:
[code=html]<input type=”button” value=”submit” onclick=”submitData()” />
[CODE]
function makeRequest()
{
var http_request = false;
if (window.XMLHttpRequest)
{ // Mozilla, Safari,…
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType(‘text/xml’);
// See note below about this line
}
}
else if (window.ActiveXObject)
{ // IE
try
{
http_request = new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
try
{
http_request = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e) {}
}
}
if (!http_request)
{
alert(‘Giving up 🙁 Cannot create an XMLHTTP instance’);
return false;
}
else
return http_request ;
}
function prepareData()
{
// get the data for the files
var titleData = document.getElementById(‘titleData’).innerHTML ;
var data = ‘var1=title.txt&data=’+ titleData ;
return data ;
}
/**
* This function will send the data to a given url
* after the request was made for ajax.
*
* @param ajaxRequest – The object that made request to the server
* @param data – The data that will be sent to the server
* @param url – The url of the file that will accept the data
*/
function postData(ajaxRequest, data, url)
{
ajaxRequest.open(‘post’, url, true);
ajaxRequest.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
ajaxRequest.onreadystatechange = function () { ajaxResult(ajaxRequest) ; } ;
ajaxRequest.send(data);
}
/**
* This function will show the output of the ajax request, which will
* be from the file / url that was connected to. The output will be
* written to a div that is already formatted using css
*
* @param ajaxRequest – The object that made request to the server
*/
function ajaxResult(ajaxRequest)
{
if(ajaxRequest.readyState == 1)
{
//You can add animated gif while loading //
document.getElementById(‘status’).innerHTML = ‘Loading…’;
}
if(ajaxRequest.readyState == 4)
{
var data = ajaxRequest.responseText;
document.getElementById(‘status’).innerHTML = data;
}
}
function submitData()
{
var requst = makeRequest() ;
var prepData = prepareData() ;
//alert(requst) ;
var url = ‘file.php’ ;
//alert(url) ;
postData(request, prepData, url) ;
}