I want to run an agent on the web. I have called that agent through Javascript. That agent is returning some value to the AJAX. In the AJAX code I am dumping this value in to the page, in an HTML element. My code is dumping the value properly, but when i try to get the value of HTML element in another java script function then it shows blank.
below is the code :
function callRunAgent()
{
//Call a javascript functiona and pass some arguments.
runAgent(strAgentName,strPgName, strLanguageName, strIDs)
}
function runAgent(strAgentName,strPgName, strLanguageName, strIDs)
if(objHTTP.readyState == 4)
{
if (objHTTP.status==200)
{
// get the XMLHttp server response and stores into a string
strLabels = objHTTP.responseText;
document.getElementById('lblNames').value = strLabels;
alert('in state change--> '+document.getElementById('lblNames').value)
}
}
}
In the stateChanged(), in alert it is showing the correct value returned from the agent, but I want to use the strLabels value in callRunAgent().
stateChanged() does not return a value.If we try to return a value from stateChanged then it shows ‘undefined’ .I have even tried make the strLabels global variableand then used it in callRunAgent(), but it does not work for me.
Can anyone tell me, if I want to use the strLabels value in my callRunAgent(), then how to use it??
Subject: Getting problem in accessing a value returned by Agent through AJAX
I think that what you are encountering has to do with the Ajax call being asynchronous.
The function stateChanged is what is called when the function return a value. The function itself does not return a value.
Hence, if you use the strLabels value immediately after issuing the Ajax call, chances are that you’ll get nothing back.
I believe I came across an earlier post asking about the Pros/Cons of using Synchronous Ajax calls so I guess you can set the call to Synchrounous.
BTW, if you are using the returning value for display purposes (strLabels seems to imply usage for labels), look into setting the innerHTML of named CSS elements.
var obj = document.getElementById(“lblUser”);
if (obj) {
obj.innerHTML = strLabels;
}
In the HTML,
User
Here the text User will get replaced by strLabels.
Subject: RE: Getting problem in accessing a value returned by Agent through AJAX
Suchita,
Well, first off you can’t do alert(strUser) because strUser is not a string. It is an object referencing the element whose ID is lblUser. If lblUser is a field, then strUser points to that field.
Andrew is correct about the timing issue. If you intend to reference strLabels in callRunAgent(), then you have to first check that a value has been returned. You should use the JavaScript setTimeout() function and wait 1,000 milliseconds (or some longer value) and check if the download was successful. Set a Boolean global variable like success = false and then change it to true when objHTTP has returned a value. The setTimeout() should be called if success still equals false.