HaunahLee
Apr 10th 2007, 5:35 pm
I have an Ajax/JS enabled page that sends a request and gets a response from the server, but I can't get it to display the result on the page.
I'm using FireBug (Firefox JS debugging extension) and the response from the server side script shows up in the debugger. But When I try to display it on the page with "alert(ajax.responseText);" I get an empty alert box.
This is the script on the main page:
<SCRIPT src='../../include/ajax/ajax_basic.js'></SCRIPT>
<SCRIPT type="text/javascript">
var ajax;
function display_body (key)
{
ajax = create_ajax(); // Create an HTTP request object nammed 'ajax'
if (ajax==null)// The creation failed
{
alert ("Browser does not support HTTP Request");
return;
}
var url="../../include/ajax/FAQ_answer.php";
url=url+"?string="+key;
ajax.onreadystatechange=update_page('faq_field', ajax);
alert(ajax.responseText);
ajax.open("GET",url,true);
ajax.send(null);
}
</SCRIPT>
And this is the script in the included file:
function update_page(id, ajax)
{
id = "'" + id + "'";
alert(ajax.responseText);
if (ajax.readyState==4 || ajax.readyState=="complete")
{
if (ajax.status == 200)
{
alert("we made contact");
document.getElementById(id).innerHTML=ajax.responseText;
}
else
{
alert("The Ready State is Four, but the response is NOT 'OK'");
}
}
}
function create_ajax()
{
ajax=null;
try
{
// Firefox, Opera 8.0+, Safari
ajax=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
ajax=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
ajax=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return ajax;
}
If someone could tell me what I'm doing wrong, it would be greatly appreciated.
Thanks!
I'm using FireBug (Firefox JS debugging extension) and the response from the server side script shows up in the debugger. But When I try to display it on the page with "alert(ajax.responseText);" I get an empty alert box.
This is the script on the main page:
<SCRIPT src='../../include/ajax/ajax_basic.js'></SCRIPT>
<SCRIPT type="text/javascript">
var ajax;
function display_body (key)
{
ajax = create_ajax(); // Create an HTTP request object nammed 'ajax'
if (ajax==null)// The creation failed
{
alert ("Browser does not support HTTP Request");
return;
}
var url="../../include/ajax/FAQ_answer.php";
url=url+"?string="+key;
ajax.onreadystatechange=update_page('faq_field', ajax);
alert(ajax.responseText);
ajax.open("GET",url,true);
ajax.send(null);
}
</SCRIPT>
And this is the script in the included file:
function update_page(id, ajax)
{
id = "'" + id + "'";
alert(ajax.responseText);
if (ajax.readyState==4 || ajax.readyState=="complete")
{
if (ajax.status == 200)
{
alert("we made contact");
document.getElementById(id).innerHTML=ajax.responseText;
}
else
{
alert("The Ready State is Four, but the response is NOT 'OK'");
}
}
}
function create_ajax()
{
ajax=null;
try
{
// Firefox, Opera 8.0+, Safari
ajax=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
ajax=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
ajax=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return ajax;
}
If someone could tell me what I'm doing wrong, it would be greatly appreciated.
Thanks!