Jump to content

XHR.readyState stuck at 1


centaur
 Share

Recommended Posts

I have the following AJAX code where I am requesting the server for some specific response once I send to it some information. The information is sent to the server correctly and the server correctly processes it and sends back the xml response to the client.But on the client side, the control never goes to the callback function: updateMsgOnBrowser().On looking a bit closely at the execution, I found that the 'req.readyState' attribute is always stuck at 1 and never takes the value 4 which is essential for the callback function to be called.Following is the code that I am using.

function loadXMLDoc(url){	var req = newXMLHttpRequest();	var handlerFunction = getReadyStateHandler(req,updateMsgOnBrowser);	alert("ready state--------before open" + req.readyState);	req.open("POST","http://localhost:8080/upload",true);	req.onreadystatechange = handlerFunction;		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");	//req.setRequestHeader("Cache-Control", "no-cache");		req.send("uploadConfig=true" + "&userName=" + document.getElementById("username").value + "&baseCategoryFile=" + document.getElementById("basecategory").value);}function updateMsgOnBrowser(xml){	alert("xsl.......");        //code for XSLT of the server response}function getReadyStateHandler(req, responseXmlHandler) {	if (req.readyState == 4) 	{			//Check that a successful server response was received		if (req.status == 200) 		{			// Pass the XML payload of the response to the handler function			responseXmlHandler(req.responseXML);		} 		else 		{			// An HTTP problem has occurred			alert("HTTP error: "+req.status);		}	}}

Can anyone here please indicate the mistake that I am making in the code above?I would be very grateful for any sort of help in the issue above. Please Help!Thanks in advance :) --Centaur

Link to comment
Share on other sites

This line:var handlerFunction = getReadyStateHandler(req,updateMsgOnBrowser);is the problem.You are executing the function right away (before the request is sent) so when the function runs the state is at 1 so nothing gets executed, making handlerFunction undefined, which makes the onreadystatechange undefined, which means there is no event handler bound to that event. Try something like this:var handlerFunction = function() { getReadyStateHandler(req,updateMsgOnBrowser); }That assigns an anonymous function to the variable handlerFunction effectively making handlerFunction an actual function.

Link to comment
Share on other sites

This line:var handlerFunction = getReadyStateHandler(req,updateMsgOnBrowser);is the problem.You are executing the function right away (before the request is sent) so when the function runs the state is at 1 so nothing gets executed, making handlerFunction undefined, which makes the onreadystatechange undefined, which means there is no event handler bound to that event. Try something like this:var handlerFunction = function() { getReadyStateHandler(req,updateMsgOnBrowser); }That assigns an anonymous function to the variable handlerFunction effectively making handlerFunction an actual function.
Thank you so much for taking your time out and answering to the thread. It has started working though now I have started getting http error 500. :) Thanks.--Centaur
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...