Return value from Jsp using Ajax
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Prissila_Aroul
Posted On:   Monday, June 19, 2006 03:38 AM

Hi, Im new to Ajax and trying to validate my form fields using client-side script and Ajax. Ajax is used to check if the fieldValue already exists . I want to return a value from the Ajax and use it to validate my form element. var returnResult = validateUsingAJAX(tableFieldId,f.fieldValue[i].value,i) ; i call a function validateUsingAJAX with respective parameters. function validateUsingAJAX(tableFieldId,val,indexValue){ var xmlHttpRequest=init(); xmlHttpRequest.open("GET", "/validateName.jsp?fieldValue="+val+"&tableFieldId="+tableFieldId, true); xmlHttpRequest.onreadystatechange=function() {alertContent   More>>

Hi,
Im new to Ajax and trying to validate my form fields using client-side script and Ajax. Ajax is used to check if the fieldValue already exists .

I want to return a value from the Ajax and use it to validate my form element.



var returnResult = validateUsingAJAX(tableFieldId,f.fieldValue[i].value,i) ;



i call a function validateUsingAJAX with respective parameters.


function validateUsingAJAX(tableFieldId,val,indexValue){
var xmlHttpRequest=init();
xmlHttpRequest.open("GET", "/validateName.jsp?fieldValue="+val+"&tableFieldId="+tableFieldId, true);
xmlHttpRequest.onreadystatechange=function() {alertContents(xmlHttpRequest)};
xmlHttpRequest.send(null)
}





function init(){
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}



The function alertContents returns well the result "true" or "false".But when i return the res,nothing happens in the calling script ,coz its always undefined.





function alertContents(xmlHttpRequest){
if(xmlHttpRequest.readyState==4){
if(xmlHttpRequest.status==200){
var res = xmlHttpRequest.responseText; return res;
}
}
}




So,is a possible solution for this! Though it can be very simple, im finding it difficult to work on and stuck with it .

Thanks ,    <<Less

Re: Return value from Jsp using Ajax

Posted By:   Anonymous  
Posted On:   Monday, June 19, 2006 08:24 AM

Yours is the case may be where we should use Ajax Synchronously, The validateUsingAJAX function fires the request and does'nt wait for the response ,so your return value is always undefined.
Try changing the xmlHttpRequest.open("GET", "/validateName.jsp?fieldValue="+val+"&tableFieldId="+tableFieldId, true); code . Change the last parameter from true to false. It will make the your request a synchrounous one, then your function will wait untill you get the response.
About | Sitemap | Contact