Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
JavaScript
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);
}

It works perfectly fine on IE. However, when tried on Firefox, the
JavaScript
oRequest.send();
fires an exception which is pretty empty and doesn't say anything:
JavaScript
e2: Failure


Any reasons why it does that on Firefox?!
Thanks in advance!
Posted
Comments
Sergey Alexandrovich Kryukov 7-Mar-13 14:27pm    
Not just Firefox. If should not generally work. As a rule of thumb, you should never ever use ActiveX objects on client side. They will never be supported on all systems, and they are generally extremely unsafe. Computer-savvy users will never use your site if they see that you are using such dirty tricks as ActiveX.

What exactly is the goal of your code? You can always find some reasonable pure-JavaScript alternatives... HTTP request is really easy thing to achieve. Now, what do you need to do with XML/HTML? Some parsing, extracting some data, what?

—SA
Daroosh 8-Mar-13 4:49am    
Well, thanks for the reply however, if you haven't noticed, I said its not working on FIREFOX which doesn't support ActiveX Objects in the first place!!

This code is meant to work across all browsers, IE (Versions 4-10), Firefox, Chrome and Safari hence, the Try/Catch Blocks to handle different browsers. As for Firefox, its only the first Try/Catch block that gets fired:
oRequest = new XMLHttpRequest();

So unfortunately, ActiveX objects have nothing to do with this code not functioning on FIREFOX!
Sergey Alexandrovich Kryukov 8-Mar-13 11:32am    
Dirty or not, but the browsers and systems are not obliged to support them, that should be enough.
XMLHttpRequest? I'll give you the link...
Done, please see my answer.
—SA

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Mar-13 14:30pm    
Of course, the idea is quite right, but where the OP can get the detail: you could reference jQuery Ajax help page and something else. Besides, we would need some more information from OP — please see my comment to the question. (Anyway, I voted 4.)
Cheers,
—SA
Prasad Khandekar 7-Mar-13 14:34pm    
Hi,

Here is the link.
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:
JavaScript
Access-Control-Allow-Origin: *

Once your Client side XMLHttpRequest see's this header in the server's response, it proceeds on by forwarding the request to the server.
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900