How to get a response from servlet via ajax?

Help with AJAX (Advice / Suggestion Request)?

  • ok here's the deal i am making a bit of a registration form. the form uses javascript for validation. in one of the validation fields it depends on the user names having unique values. there for of course you know we'd have to enter our ajax coding: var blnStatus; function checkFinancialInstutition(financialInsti… { if(objAJAX = new XMLHttpRequest()) { }else if(objAJAX = new ActiveXObject("Microsoft.XMLHttp")) { }else if(objAJAX = new ActiveXObject("msxml.XMLHttp")) { }else{ } objAJAX.open("GET", "cInstitution.asp?institution=" + financialInstitution, true); objAJAX.send(null); objAJAX.onreadystatechange = function() { if(objAJAX.readyState == 4) { xResults = objAJAX.responseText; if(xResults == "True") blnStatus = true; else blnStatus false; } } return blnStatus; } then we'd have the part which utilizes this function to test it: <script> function FinancialInstitutionClicked() { xInstitution = document.getElementById("errReport"); institutionName = document.getElementById("FinancialInstit… if (institutionName.value != "") { if (checkFinancialInstutition(institutionNa… { xInstitution.innerHTML = "\n<UL><li onclick=\"changefocus('FinancialInstitut… Financial Institution Name Already Exists</UL>"; xInstitution.style.display = "block"; } } } </script> of course we'd have our fields. Now here's the problem: when user enters a value to test. the code AJAX it to the ASP and the ASP which in turn tells our form if the name exists. the problem is the code doesn't wait for the ajax to return a value. so because AJAX is returning the value late. blnStatus never went from undefined value to a true or a false value. because the code only test to see if the value is not true. the name is always not going to exist. How can i either get my results immediately or wait on the results prior. maybe i could put a loop while( (blnStatus != true) || (blnStatus != false) ) { ...stall code here } ..return here but as programmers we like to be prepared for whatever. if they remove the cInstitution.asp file. we'd have an error and the code would be waiting for a response all day. So i'm just basically asking any ideas to correcting or getting around this problem.. will reward +10 points to most helpful answer. thanks in advanced.........

  • Answer:

    Review your AJAX using the reliable Sajax (AJAX in php). Get many samples from free scripts at http://web2coders.com. One file is important: sajax.php The rest is "how to use it" ... :-) The advantage is that Sajax.php will be on YOUR server, and you will not depend on anybody... The script "lesson 3: use of AJAX" is most appropriate and elementary.

Andron Smith at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.