Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ajax responseXML problem

 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was going through "Head first Ajax". There in chapter 2, we are having a registration page. Once we enter username field we make a check thorough ajax request object.

The flow is somewhat as below
registration.html--->javascript(var url = "checkName.php?username=" + username;)--->checkName.php.

Now in our checkName.php we are creating an array, and checking the parameter send by request URL againt it as below-:



where as in our javascript we are looking for the response as-:


Now this is fine with php as response from server will be just "Okay" or "denied". But as i dont know php am using a Jsp in place of it as shown-:


and my javascript as-:


But now the problem is that unlike php response of just "okay" or "denied" i shall be getting full jsp as a response with "exists" in body tag if user exists.
so this can't match the condition i have written in my javascript (request.responseText!= "exists").
Is there a way to overcome this? Using request.responseXML instead of request.responseText ?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are getting the full response because that is what you are sending down to the page.

You probably want to call a servlet instead of a jsp.

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's fine to use JSP. Remember a JSP does not have to be a full HTML page. It can be whatever fragment of text you want it to be.
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to use request.responseXML.document.getElementById("show"). But this not working too. Below was the JSP page
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is it you are trying to get from the JSP? If all you need is some simple text, why do you have all that other crap like <body> in there?
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,
Even if i remove the body and html tags still i need the import tag for JSTL. And the logic to search name in my array as below-:


I tried to capture the output to request.responseText through alert and it shows exists but have line spacing preceding and following it.
For test i removed all logic and just wrote exist in jsp then it works fine . But still i have to go through the loop to search the user or else will have to change the code somewhat if we cant have just exists as response from this JSP.
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey well i got a idea. Trim the whitespace before and after the string. It worked now-:



and the trim function as-:


Thanks to all for help. Bear thanks for giving the idea of removing all tags
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic