File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Ajax with jQuery and servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Ajax with jQuery and servlets" Watch "Ajax with jQuery and servlets" New topic
Author

Ajax with jQuery and servlets

Dani Thorp
Greenhorn

Joined: Apr 29, 2008
Posts: 11
Good evening,
I'm trying to get a response from an Ajax call using jQuery and servlets, but somehow I always get an error.

jQuery code:


Servlet code:


If I type the servlet url I get the "You're in!" message, but when I try to use jQuery routine I get "Error requesting page http://localhost:8080/investking/servlet/login".
Does anyone know how could I do it? Or at least a better way to communicate jQuery with servlets?

Many thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

Do you get the same issue with:

?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dani Thorp
Greenhorn

Joined: Apr 29, 2008
Posts: 11
Yes, I also get "Error requesting page /investking/servlet/login". I really don't know where to search, I've also tried with JSON, but the problem should be in the request, not in the format.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

OK, that sounds really odd and unrelated to jQuery. At this point I'd run the page under Firefox with Firebug installed to see what other information can be gathered about the nature of the error. Firebug is absolutely essential to diagnosing Ajax problems.
Dani Thorp
Greenhorn

Joined: Apr 29, 2008
Posts: 11
Uhm, I'm running Firefox 3.0b5 under Ubuntu and Firebug is not ready yet for that version. I've tried to install Firefox 2 but I can't make it work.
I suppose I'll have to try with another library... any suggestion?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

Another library will yield the same results. I'm pretty sure that this is an Ajax issue not a jQuery issue.

Do you have no tools at your disposal that will show you the request and response details? TamperData? other Ubuntu tools?

Btw, just to be sure about things:

URL you typed into the browser:
http://localhost:8080/investking/servlet/login

URL on Ajax request:
/investking/servlet/login

And the page containing the Ajax request is part of the same web app. Correct?
Dani Thorp
Greenhorn

Joined: Apr 29, 2008
Posts: 11
Wow Bear, that TamperData is amazing!
When I press on 'Details' on the first message I can see that the servlet sends back the "You're in!" message, but for some reason seems that jQuery takes it as an error.
That first message comes with the 'LOAD_BACKGROUND' flag, but just after it comes another one from http://localhost:8080/investking/ with the load flags 'LOAD_DOCUMENT_URI' and 'LOAD_INITIAL_DOCUMENT_URI'. This second message makes the page reload.
Do you know why it happens?
Dani Thorp
Greenhorn

Joined: Apr 29, 2008
Posts: 11
Oh, I forgot to tell you that you're right: the URL I typed into the browser was http://localhost:8080/investking/servlet/login and the URL on Ajax request is /investking/servlet/login. And the page with the request is in the same webapp as the servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

OK, that's really odd. I've never seen jQuery choke on a valid response. What version of jQuery are you using? I'll see if I can reproduce your issue.
Dani Thorp
Greenhorn

Joined: Apr 29, 2008
Posts: 11
I'm using jQuery 1.2.3, but don't worry about it. You've already done a lot to help me, I'll try with other jQuery versions and JavaScript libraries.
Thank you indeed for your help!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

If you do find it's an issue with jQuery, please let me know so I can file an appropriate bug report.
Dani Thorp
Greenhorn

Joined: Apr 29, 2008
Posts: 11
I really don't know why it didn't work, but now I'm using the jQuery Forms plugin and it's working like a charm
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61209
    
  66

The Form plugin rocks! I especially like the way it can take a "normal" form and Ajax-ify it for you with a single line of JS.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

http://www.getfirebug.com/releases/

Install the 1.2 Alpha and it will work, enough. I had the same issues trying to install FF2 and the FireBug beta is a POS. Alpha seems to be working for me though.


GenRocket - Experts at Building Test Data
Derrick Shoemake
Greenhorn

Joined: May 20, 2009
Posts: 1
Sorry this is late, but if you type the URL into the browser it should run the doGet method of a servlet, but it you submit a form, it should be running the doPost method. A simple call to the doGet from the doPost should fix this problem.



EDIT:

Opps, I thought I read it as $.post instead of $.get, I should learn to read one of these days...nevermind
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I would say you are late, over a year late.

Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ajax with jQuery and servlets