tags:

views:

118

answers:

5

Could someone please tell me what is wrong with this code? It works fine in IE8 but not in FF3.

$.get("http://google.com/", function(data) { alert(data); });

It shows me the alert window but it's empty.

+8  A: 

I believe it has to do with the cross domain AJAX restrictions between the two browsers. FireFox is more strict (secure) when it comes to AJAX. Your code violates FireFox's "Same Origin Policy"

Hope this helps!

Aardvark
The Same Origin Policy isn't a Firefox-only feature, all modern browsers have this. IE8's Same Origin Policy is the same as Firefox's, unless you disable it for a specific "Security Zone".
Andy E
True, I guess I should have been a little more specific. I believe its much harder to set Firefox to allow Access Across Domains which requires a proxy to be set up. This is what I meant when I said it was more secure, Internet Explorer all you have to do is check the box without necessarily knowing what it is
Aardvark
+2  A: 

You cannot use AJAX to send a request to a different domain.

In IE, you apparently enabled this option.

This is a bad security hole.

You should re-disable it immediately

Go to Internet Options, Security, Custom Level, Miscellaneous, and disable Access Data Sources across Domains.

SLaks
Lol that font just looks dangerous
Aardvark
Quite, I actually went to check MY settings after reading that!
Eton B.
A: 

AJAX doesn't allowed cross site requests like that, for security purposes -- I'm surprised it works in IE. It should work from your own domain, though.

Ian Henry
I reset IE security settings, now it's not working anymore.
David Weng
+6  A: 

That code shouldn't work in Firefox or IE, due to the same origin policy. Chances are, you've set your security settings for the site's zone to be able to access data across domains:

To change this behaviour (and you probably should), go to Internet Settings -> Security -> Choose the zone for the current site -> Choose Custom level...

Andy E
+3  A: 

Please read the documentation for $.get() (or really any other jQuery AJAX call).

http://api.jquery.com/jQuery.get/

It specifies the following lower down on the page: Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

This is likely the source of your problem.

Ender