XMLHttpRequest无法在Firefox上运行 [英] XMLHttpRequest not working on Firefox

查看:84
本文介绍了XMLHttpRequest无法在Firefox上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿那里,



我一直试图打开一个套接字,使用以下代码向本地服务器发送一个基本的GET请求:

Hey there,

I''ve been trying to open a socket to send a basic "GET" request to a local server using the following piece of code:

var oRequest = null;

try {
	oRequest = new XMLHttpRequest();
}
catch (e) {
	oRequest = null;
}

try {
	if ( oRequest == null ) oRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
	oRequest = null;
}

try {
	if (oRequest == null) oRequest = new ActiveXObject("Msxml2.XMLHTTP.6.0");
}
catch (e) {
	oRequest = null;
}

try {
	oRequest.open( "CAT", 'http://localhost:8484', false);
}
catch (e) {
	alert( 'e1:' + e.message + ' ' + e.description);
}

try {
        oRequest.send();
}
catch (e) {
	alert('e2:' + e.message);
}



它在IE上运行得非常好。但是,在Firefox上试用时,


It works perfectly fine on IE. However, when tried on Firefox, the

oRequest.send();

会触发一个非常空的异常并且没有说什么:

fires an exception which is pretty empty and doesn''t say anything:

e2: Failure





为什么在Firefox上这样做的原因?!

提前致谢!



Any reasons why it does that on Firefox?!
Thanks in advance!

推荐答案





我建议您尝试使用JQuery API因为它可以保护您免受浏览器特定的实现,并且已知与浏览器无关。它还具有接收成功和失败回调的功能。我已经使用了很长时间了。



问候。
Hi,

I will suggest that you try JQuery API as it will shield you from browser specific implementations and is known to be browser independent. It also has capability for receiving a success & failure callbacks. I have been using it for quite a long time.

regards.


找到问题的解决方案。 Firefox不允许您的XMLHttpRequest访问跨域以获取数据。因此,为了通过这个次要的安全检查,从服务器向您的XMLHttpRequest REPLY添加以下标题:

Found the Solution to my problem. Firefox doesn''t allow your XMLHttpRequest to access cross domains to fetch data. Hence, in order to by pass this minor security check, add the following header to your XMLHttpRequest REPLY from the server:
Access-Control-Allow-Origin: *



一旦您的客户端XMLHttpRequest在服务器的响应中看到此标头,它就会继续将请求转发给服务器。


Once your Client side XMLHttpRequest see''s this header in the server''s response, it proceeds on by forwarding the request to the server.


请参阅我对该问题的评论。

对于 XMLHttpRequest ,请查看此代码:

http://en.wikipedia.org/wiki/XMLHttpRequest#Another_method_to_set_XMLHttpRequest_for_all_browsers [ ^ ]。



尽管如此,我坚持认为使用ActiveX真的很糟糕。所有系统上的所有浏览器都不会支持它,因为这是一个专有的东西,众所周知是不安全的。精通计算机的用户如果看到你使用这些东西就不会使用你的网站。



我不知道你的目标细节,但我认为你可以找到一个很好的JavaScript解决方案。



-SA
Please see my comments to the question.
For XMLHttpRequest, look at this code:
http://en.wikipedia.org/wiki/XMLHttpRequest#Another_method_to_set_XMLHttpRequest_for_all_browsers[^].

Nevertheless, I insist that using ActiveX is really bad. It will never be supported by all browsers on all systems, as this is a proprietary thing, notoriously unsafe. Computer-savvy users'' won''t use your site if that see that you use such things.

I don''t know the detail of your goals, but I think you can find a nice JavaScript-only solution.

—SA


这篇关于XMLHttpRequest无法在Firefox上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆