dcsimg
Updating global variable fails
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Akin_Sarigul
Posted On:   Saturday, August 19, 2006 05:33 PM

Hi all, I'm new to client-side scripting. I'm trying to use ajax to get dynamic content from my server. Here is my code: [On HTML File] ... [script language="JavaScript" src="test.js"][/script] ... ... [body onLoad="init();"] ... [On JavaScript File (test.js)] var httpRequest = false; var url = "do.php"; var params = "x=1&y=2"; var xmlResponse = null; function init() { alert(xmlResponse); //displays null makePostRequest(url, params); alert(xmlResponse); //still displays null   More>>

Hi all,

I'm new to client-side scripting. I'm trying to use ajax to get dynamic content from my server. Here is my code:



[On HTML File]


			
...
[script language="JavaScript" src="test.js"][/script]
...
...
[body onLoad="init();"]
...



[On JavaScript File (test.js)]


			

var httpRequest = false;
var url = "do.php";
var params = "x=1&y=2";
var xmlResponse = null;

function init() {
alert(xmlResponse); //displays null

makePostRequest(url, params);

alert(xmlResponse); //still displays null!!!
}

function makePostRequest(url, params) {
http_request = new XMLHttpRequest();
...
...
...
http_request.onreadystatechange = updateVars;
...
...
}

function updateVars() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
xmlResponse = http_request.responseXML;

//this works fine:
//alert(xmlResponse);
}
}
}



As you see, my global variable named xmlResponse is supposed to be updated after makePOSTRequest function call which executes updateVars(). But it is not updated?! Why does this happen?


Any help will greatly appreciated. Thanks all...

   <<Less

Re: Updating global variable fails

Posted By:   Robert_Lybarger  
Posted On:   Saturday, August 19, 2006 06:39 PM

My only suggestion here, not being an AJAX guy myself, is to check that the server is returning the document with content type set to text/xml instead of possibly text/html or text/plain. This may be necessary for the 'responseXML' property of the request object to be valid / not-null. If you've checked that, someone else will need to chime in.
About | Sitemap | Contact