views:

698

answers:

6

Hi guys, I have small ajax problem related to cross domain as i see it.

On localmachine i created html example with some ajax: in registration text field user types 'username', on every keystroke ajax sends it to local Tomcat, where servlet checks if that username is already used and sends 'taken' reponse back.

No problem on localhost at all. As soon as i type used 'username' servlet sends 'taken' response and browser displays it.

But, when i put test html page with ajax on remote machine (some free hosting on remote network) that sends validation request on my localhost Tomcat, connection is made, in Tomcat console i see request comming, and in firebug in Mozzila this is Console ouput:

GET http://89.216.182.25:8080/Dinamicki1/UsernameServlet?username=zik 200 OK

...but in response tab there is not servlet response 'taken' and message in firebug is in red color

So servers communicate well, no firewall problems, response is 200 OK
But response body is empty.

Any ideas what this red messages in firebugs are?

Thank you very much in advance.

And if anyone can recommend a some serious ajax tutorial for java it will be highly appreciated :)

A: 

You cannot use AJAX to read replies from other domains.

Your HTML must be on the same server (and same domain, port, and protocol) as the AJAX servlet.

SLaks
+1  A: 

The 200 status reported in Firebug does not indicate the validity of the cross-domain ajax call, be it successful or not.

You might want to try using a proxy method to perform the call.

E.g. JavaScript: Use a Web Proxy for Cross-Domain XMLHttpRequest Calls

o.k.w
+1  A: 

You need to use a domain-relative URL in your Ajax request:

/Dinamicki1/UsernameServlet?username=zik

Or a context-relative URL (assuming that the page is served from /Dinamicki1):

UsernameServlet?username=zik

With regard to "Ajax tutorial for Java", well there's actually not really one. Those are just two separate technologies. I would however recommend to get yourself started with jQuery (for the client side) and Google Gson (for the server side) and JSON (as communication language between client and server).

BalusC
@BalusCI guess using domain-relative URL will not help me if html with ajax resides on that free hosting domainand servlet that is called from ajax is still on my local machine ?So I either use this proxy solution or i move my servlett on same machine where html and ajax is?
reg
You cannot fire cross-domain Ajax requests. Certainly both the ajax source and target ought to be on the same domain. A domain-relative URL is easiest way to ensure this and to spot potential bugs. A proxy servlet using `java.net.URLConnection` may indeed solve your problem. Better would be still to host all at the same machine.
BalusC
A: 

thank you all very much for answers you provided :)

cheers!

reg
A: 

Or use JSONP instead of JSON, it will solve your problem.

MBela
A: 

I have found how to solve it: http://www.vogella.de/articles/ApacheTomcat/article.html#tomcat_httpserver

1 step: "To allow directory browsing via Apache Tomcat change the parameter "listings" in the file conf/web.xml from false to true."

2 step: call your page not as "C:/Documents and Settings/.../page.html" but as localhost:8080/your_servlet_name (page is better to be named as "index.html"). So you will be able to make AJAX requests to "localhost:8080/your_servlet_name/something_else" and this will work!

Gleb