ajax problem - 200 OK in firebug but red message w

2019-04-03 06:58发布

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 :)

6条回答
闹够了就滚
2楼-- · 2019-04-03 07:07

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).

查看更多
做个烂人
3楼-- · 2019-04-03 07:08

A solution that worked for me was that I had to add "www" to the url! I was using URL Rewrite, so every URL that I had (image, js, get, load, post), I needed to use full url, but it was missing "www"!

查看更多
姐就是有狂的资本
4楼-- · 2019-04-03 07:11

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

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-04-03 07:18

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.

查看更多
霸刀☆藐视天下
6楼-- · 2019-04-03 07:27

For me, It was web api(c# .NET) request and cors was not enabled. Added header for cors on controller and it solved the problem.

[EnableCors(origins: "*", headers: "*", methods: "*")]
查看更多
戒情不戒烟
7楼-- · 2019-04-03 07:32

I figured out how to solve it from this site:

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

  2. Call your page not as C:/Documents and Settings/.../page.html but as localhost:8080/your_servlet_name (page is better named index.html).

This way, you will be able to make AJAX requests to localhost:8080/your_servlet_name/something_else.

查看更多
登录 后发表回答