java ajax 问题 - firebug 中的 200 OK 但没有响应正文的红色消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2219274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:07:39  来源:igfitidea点击:

ajax problem - 200 OK in firebug but red message with no response body

javaajaxfirebugcross-domain

提问by reg

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

正如我所见,我有一个与跨域相关的小 ajax 问题。

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.

在本地机器上,我用一些 ajax 创建了 html 示例:在注册文本字段中,用户键入“用户名”,每次击键时,ajax 都会将其发送到本地 Tomcat,在那里 servlet 检查该用户名是否已被使用并发送回“已获取”响应。

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

在本地主机上根本没有问题。只要我输入“用户名”,servlet 就会发送“已接受”响应,浏览器会显示它。

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:

但是,当我在我的本地主机 Tomcat 上发送验证请求的远程机器(远程网络上的一些免费托管)上放置带有 ajax 的测试 html 页面时,就建立了连接,在 Tomcat 控制台中我看到请求来了,在 Mozzila 的 firebug 中这是控制台输出:

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

...但在响应选项卡中,没有 servlet 响应“已采取”,并且萤火虫中的消息为红色

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

所以服务器通信良好,没有防火墙问题,响应是200 OK
但响应体为空。

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

如果有人可以为 Java 推荐一些严肃的 ajax 教程,我们将不胜感激:)

采纳答案by BalusC

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

您需要在 Ajax 请求中使用域相关 URL:

/Dinamicki1/UsernameServlet?username=zik

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

或者一个上下文相关的 URL(假设页面是从 提供的/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).

关于“Java Ajax 教程”,实际上并没有真正的教程。这只是两种不同的技术。但是,我建议您开始使用jQuery(用于客户端)和Google Gson(用于服务器端)和JSON(作为客户端和服务器之间的通信语言)。

回答by SLaks

You cannot use AJAX to read replies from other domains.

您不能使用 AJAX 读取来自其他域的回复。

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

您的 HTML 必须与 AJAX servlet 位于同一服务器(以及相同的域、端口和协议)上。

回答by o.k.w

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

Firebug 中报告的 200 状态并不表示跨域 ajax 调用的有效性,无论是否成功。

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

例如JavaScript:使用 Web 代理进行跨域 XMLHttpRequest 调用

回答by Gleb

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.htmlbut as localhost:8080/your_servlet_name(page is better named index.html).

  1. “要允许通过 Apache Tomcat 进行目录浏览,请将文件 conf/web.xml 中的参数“listings”从 false 更改为 true。”

  2. 将您的页面称为C:/Documents and Settings/.../page.html而不是作为localhost:8080/your_servlet_name(页面更好地命名index.html)。

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

这样,您就可以向localhost:8080/your_servlet_name/something_else.

回答by Rafael Moni

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"!

一个对我有用的解决方案是我必须在网址中添加“www”!我正在使用 URL 重写,所以我拥有的每个 URL(图像、js、获取、加载、发布),我需要使用完整的 url,但它缺少“www”!

回答by Chaitanya Gadkari

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.

对我来说,这是 web api(c# .NET) 请求,并且没有启用 cors。为控制器上的 cors 添加了标头并解决了问题。

[EnableCors(origins: "*", headers: "*", methods: "*")]