164

I'm trying to make a simple cross-origin request, and Firefox is consistently blocking it with this error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at [url]. This can be fixed by moving the resource to the same domain or enabling CORS. [url]

It works fine in Chrome and Safari.

As far as I can tell I've set all the correct headers on my PHP to allow this to work. Here's what my server is responding with

HTTP/1.1 200 OK
Date: Mon, 23 Jun 2014 17:15:20 GMT
Server: Apache/2.2.22 (Debian)
X-Powered-By: PHP/5.4.4-14+deb7u8
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Content-Type
Access-Control-Request-Headers: X-Requested-With, accept, content-type
Vary: Accept-Encoding
Content-Length: 186
Content-Type: text/html

I've tried using Angular, jQuery, and a basic XMLHTTPRequest object, like so:

var data = "id=1234"
var request = new XMLHttpRequest({mozSystem: true})
request.onload = onSuccess;
request.open('GET', 'https://myurl.com' + '?' + data, true)
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
request.send()

...and it works in every browser except Firefox. Can anyone help with this?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Godwhacker
  • 3,624
  • 3
  • 15
  • 23
  • Can you provide a full trace of all requests and responses? Or provide a test site at least? If the website accessing the remote origin http, https, file? `mozSystem` is not supported for regular websites just packaged [tag:firefox-os] apps, so drop that option. – nmaier Jun 24 '14 at 04:29
  • 1
    @nmaier Headers when sending as POST rather than GET: `Accept application/json, text/plain, */*` `Accept-Encoding gzip, deflate` `Accept-Language en-US,en;q=0.5` `Content-Length 35` `Content-Type application/x-www-form-urlencoded; charset=UTF-8` `Host [url]` `Origin [url]` `Referer [referrer url]` `User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:30.0) Gecko/20100101 Firefox/30.0` Error: `Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at [url]. This can be fixed by moving the resource to the same domain or enabling CORS. [url]` – Godwhacker Jun 24 '14 at 08:49
  • 2
    @nmaier Actually it appears this is some sort of problem with the security certificate on the site I'm accessing- Firefox is just giving a spurious error. If I add the site to the list of exceptions it works fine. – Godwhacker Jun 24 '14 at 09:12

17 Answers17

175

Turns out this has nothing to do with CORS- it was a problem with the security certificate. Misleading errors = 4 hours of headaches.

Godwhacker
  • 3,624
  • 3
  • 15
  • 23
  • 4
    Just had this exact problem, the error is too generic and on top of that Chrome and Firefox use different certificate stores, so its harder to debug. I should have suspected that when my proxy didn't capture any OPTIONS requests (it was breaking on the SSL handshake). – Daniel Correia Jun 29 '14 at 21:45
  • 5
    Godwhacker, I am into the same problem, perhaps. How did you find out that it was the security certificate? Where can you see this? – Leo Jan 19 '15 at 02:18
  • Unfortunately as it was seven months ago I can't remember- I think I noticed something when I was logging in to the server. Check that it's up to date and that the domains match. – Godwhacker Jan 20 '15 at 07:49
  • For me, It was because I had some characters omitted in the intermediate certificate, "-----BEGIN CERTIFICATE-----". I hope this helps someone out there in the field. – Won Jun Bae Mar 03 '15 at 01:36
  • 19
    In my case I had an angular app making calls to another server, with both using internally signed certificates. However, Firefox doesn't automatically trust the cert because it isn't recognized by a public authority. So I needed to make sure the certs for both servers were added as exceptions in Firefox before this issue went away. – Sam Storie Jun 30 '15 at 20:50
  • 11
    For me the fix was to set `withCredentials=true` on the XHR instance; otherwise Firefox failed to use the client cert when making the request (worked fine in Chrome, however). – clint Dec 04 '15 at 16:57
  • 1
    Comment by @SamStorie sounds more like answer than this answer. Thank you – Naga Feb 27 '19 at 06:23
  • 1
    My response headers had special characters which were causing an issue fixing that solved my problem. – Gagan Babber Aug 19 '19 at 06:01
  • in my case the `Remote Address` is going to 443 port with http request – void May 23 '20 at 06:56
  • Can you update your answer to explain how you found the error and fixed it? – Rylan Schaeffer Feb 10 '22 at 01:28
  • @RylanSchaeffer it was nearly eight years ago so I'm afraid I can't remember exactly how I worked it out... – Godwhacker Feb 23 '22 at 16:41
  • I tried everything from security certificate, Firefox exceptions, blockers but still I was unable to resolve this issue. I was running an express server at port **8080** locally and was receiving the above CORS error when I was trying to access the data from my **front-end app through Firefox** but surprisingly **didn't receive it in Chrome**. Coming to the issue, the culprit was **another service** on my system which was **utilizing the same port 8080**. So, check whether anything else is running on the same port. Also, It would be good if this question was still open. – Ashfaq nisar Oct 24 '22 at 06:47
43

I found that my problem was that the server I've sent the cross request to had a certificate that was not trusted.

If you want to connect to a cross domain with https, you have to add an exception for this certificate first.

You can do this by visiting the blocked link once and addibng the exception.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Cracker0dks
  • 2,422
  • 1
  • 24
  • 39
  • 8
    That doesn't particularly help anyone visiting your site, unless you're happy put a message on there saying 'If you're using Firefox, please go to this site and add it to your list of exceptions' – Godwhacker Jun 24 '15 at 08:55
  • 1
    @Cracker0dks "visit the blocked link once and add the exception" could you elaborate as to where to add the exception? I'm using Firefox Quantum. TIA – Paul Apr 18 '18 at 00:19
  • 3
    you surf to the link firefox is complaining about direktly. Then you get the certificate warning. Allow the certificate. Visit your primary site again. – Cracker0dks Apr 19 '18 at 14:48
  • 1
    thank you, I browse and trust the api domain on firefox, then the angular successfully send and receive without error – Keith POON Jun 21 '22 at 08:25
41

I came across this question having found requests in Firefox were being blocked with the message:

Reason: CORS request did not succeed

After pulling my hair out I found out that a newly installed Firefox extension, Privacy Badger, was blocking the requests.

If you come to this question after scratching your head, try checking to see what extensions you have installed to see if any of them are blocking requests.

See Reason: CORS request did not succeed on MDN for details.

awendt
  • 13,195
  • 5
  • 48
  • 66
Professor of programming
  • 2,978
  • 3
  • 30
  • 48
  • how did you figure out which extension is blocking the requests? I am having the same problem, as the request succeeds in a Firefox with a new profile (so no extensions) – Ciprian Tomoiagă Aug 13 '19 at 16:43
  • 1
    I clicked each extension to see if there were any mentions of blocking content for the particular site I was having an issue with. Ghostery had an entry so I marked the site as trusted, reloaded the page, and the requests succeeded. – Professor of programming Aug 13 '19 at 17:05
  • 1
    Thank you! I've also discovered the extension Project Insight which gives an overview of all extensions' permissions. https://addons.mozilla.org/en-US/firefox/addon/project-insight/ – Ciprian Tomoiagă Aug 14 '19 at 07:28
  • 1
    @awendt Oof. Privacy Badger for me also. Thanks. – Jason Leach Jun 16 '20 at 01:31
  • In my case I'd turned on Firefox Enhanced Tracking Protection, I then removed it for sites that I trusted (development sites) and it fixed the errors I was getting. Phew I thought we'd broken the site for Firefox :-/ – The Coder Jan 16 '22 at 23:28
39

If you don't have a 'real' certificate (and thus using a self-signed one), in Firefox you can go to:

Options > Privacy & Security > (scroll to the bottom) View Certificates > Servers > Add Exception...

There, fill in the location, eg: https://www.myserver:myport

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Hypenate
  • 1,907
  • 3
  • 22
  • 38
  • 2
    this worked for me when my CORS works in all other browsers except Firefox with the error -> Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:44304/v1/search. (Reason: CORS request did not succeed). – JGilmartin Apr 05 '20 at 18:18
  • @Hypenate: Thanks for the solution. It solved Firefox problem. But it is not working for any of the mobile browser. Could you please help me in solving this issue. – Vikash Anand Oct 06 '20 at 09:44
17

I've found solution after 2 days :(.

Important note: when responding to a credentialed request, server must specify a domain, and cannot use wild carding.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials

Hoàng Quân
  • 171
  • 1
  • 2
4

Just a word of warnings. I finally got around the problem with Firefox and CORS.

The solution for me was this post

Setting CORS (cross-origin resource sharing) on Apache with correct response headers allowing everything through | Benjamin Horn

However Firefox was behaving really, really strange after setting those headers on the Apache server (in the folder .htaccess).

I added a lot of console.log("Hi FF, you are here A") etc to see what was going on.

At first it looked like it hanged on xhr.send(). But then I discovered it did not get to the this statement. I placed another console.log right before it and did not get there - even though there was nothing between the last console.log and the new one. It just stopped between two console.log.

Reordering lines, deleting, to see if there was any strange character in the file. I found nothing.

Restarting Firefox fixed the trouble.

Yes, I should file a bug. It is just that it is so strange so don't know how to reproduce it.

NOTICE: And, oh, I just did the Header always set parts, not the Rewrite* part!

Leo
  • 4,136
  • 6
  • 48
  • 72
4

For posterity, also check server logs to see if the resource being requested is returning a 200.

I ran into a similar issue, where all of the proper headers were being returned in the pre-flight ajax request, but the browser reported the actual request was blocked due to bad CORS headers.

Turns out, the page being requested was returning a 500 error due to bad code, but only when it was fetched via CORS. The browser (both Chrome and Firefox) mistakenly reported that the Access-Control-Allow-Origin header was missing instead of saying the page returned a 500.

Jack Stouffer
  • 352
  • 1
  • 10
3

To debug, check server logs if possible. Firefox returns CORS errors in console for a whole range of reasons.

One of the reasons is also uMatrix (and I guess NoScript and similar too) plugin.

johndodo
  • 17,247
  • 15
  • 96
  • 113
3

Ubuntu Firefox giving CORS failed error when my request took more than 10 seconds to process.

Nothing to do with CORS. Problem is with ubuntu firefox configuration.

I have updated network.notify.changed to false which made this fix.


Mozilla Bugs Reference:

https://bugzilla.mozilla.org/show_bug.cgi?id=1238377

https://bugzilla.mozilla.org/show_bug.cgi?id=1235509

Ehsaan Israr
  • 589
  • 1
  • 4
  • 14
2

In my case it was my ADBLOCKER! For some reason it was enabled on my localhost and causing this error in Firefox.

Disabling it or uninstalling the plugin should fix this.

In your case, it may not be an adblocker but another Firefox plugin. Test it out in incognito first with no plugins to determine if that is the issue and then systematically disable the plugins until you find the culprit.

Christian Heath
  • 213
  • 3
  • 8
  • In my case, after enabling XHR messages in the Firefox developer console, I saw the request was blocked by and addon. Indeed there was an exclamation mark on the facebook container plugin button. I had to "allow site in facebook container" to get it working. I have to say that my URL request was to facebook basic display API, hope it helps someone. – Angel Auñón Jan 22 '22 at 12:06
1

Just add

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

to the .htaccess file in the root of the website you are trying to connect with.

Sergey
  • 7,985
  • 4
  • 48
  • 80
Gareth
  • 35
  • 1
  • 3
    That's fine if you want to set it to accept requests from anywhere; not fine if you don't, which is the entire point of the header. – Godwhacker Jul 11 '18 at 10:54
1

For me, turns out I was setting Access-Control-Allow-Origin response header to a specific (and the correct) host.com but it had to be returned as http://host.com instead. What does firefox do? It silently swallows the GET request and returns a status 0 to the XHR, with no warnings output to the javascript console, whereas for other similar failures it would at least say something. Ai ai.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
  • 1
    Seems the scheme is expected, looking at the examples at https://www.w3.org/TR/cors/ but anyways thanks, I was doing the wrong thing too, and yes, the Firefox error message is lacking. – Richard Green May 24 '20 at 03:40
1

Try this, it should solve your issue

  1. In your config.php, add www pre in your domain.com. For example:

    HTTP define('HTTP_SERVER', 'http://domain name with www/');
    HTTPS define('HTTPS_SERVER', 'http://domain name with www/');
    
  2. Add this to your .htaccess file

    RewriteCond %{REQUEST_METHOD} OPTIONS RewriteRule ^(.*)$ $1 [R=200,L]
    
Gary
  • 13,303
  • 18
  • 49
  • 71
1

In my case CORS error was happening only in POST requests with file attachments other than small files.

After many wasted hours we found out request was blocked for users who were using Kaspersky Total Control.

It's possible that other antivirus or firewall software may cause similar problems. Kaspersky run some security tests for requests, but omits them for websites with SSL EV certificate, so obtaining such certificate should resolve this issue properly.

Disabling protection for your domain is a bit tricky, so here are required steps (as for December 2020): Settings -> Network Settings -> Manage exclusions -> Add -> your domain -> Save

The good thing is you can detect such blocked request. The error is empty – it doesn't have status and response. This way you can assume it was blocked by third party software and show some info.

Dharman
  • 30,962
  • 25
  • 85
  • 135
pwkc
  • 479
  • 7
  • 19
0

I faced similar problem, and I think is valid to be registered how I fixed it:

I have a system built basically over Symfony 3. For self learn and performance purposes I decided to write few scripts using GoLang, also, an API with public access.

My Go API expects Json format params, and also return Json format response

To call those GoApi's I am using, most, $.ajax ( jQuery ) The first test was a deception: the (un)famous "Cross-Origin Request Blocked" pop up ! Then, I tried to set the "Access-Control-Allow-Origin: *" On apache conf, htaccess, php, javascript and anywhere I could find on google !

But, even, same frustrating error !!!

The Solution was simple : I had to make "POST" requests instead "GET" .

To achieve that I had to adjust both, GoLang and JavaScript to use GET ! Once it done, no more Cross-Origin Request Blocked for me !!!

Hope it Helps

PS:

I am using apache and Vhost, on Directory Block I have

  Header always set Access-Control-Allow-Origin "*"
  Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"

Remember : "*" means that you will accept requests from anyone !!! (Which may be a security lack ) In my case it is ok, because it will be an public API

PS2: My Headers

Response headers

Access-Control-Allow-Credentials    true
Access-Control-Allow-Headers    Authorization
Access-Control-Allow-Methods    GET, POST, PUT
Access-Control-Allow-Origin http://localhost
Content-Length  164
Content-Type    application/json; charset=UTF-8
Date    Tue, 07 May 2019 20:33:52 GMT

Request headers (469 B)

Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection  keep-alive
Content-Length  81
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Host    localhost:9003
Origin  http://localhost
Referer http://localhost/fibootkt/MY_app_dev.php/MyTest/GoAPI
User-Agent  Mozilla/5.0 (Macintosh; Intel …) Gecko/20100101 Firefox/66.0
Diego Favero
  • 1,969
  • 2
  • 22
  • 32
0

If the aforementioned answers don't help then check whether the backend server is up and running or not as in my case the server crashed and this error turns out to be totally misleading.

Neeraj Sewani
  • 3,952
  • 6
  • 38
  • 55
-5

The files are self explanatory. Make a file, call it anything. In my case jq2.php.

<html>
<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
    // document is made ready so that the program starts when we load this page       
    $(document).ready(function(){

        // it tells that any key activity in the "subcat_search" filed will execute the query.
        $("#subcat_search").keyup(function(){

            // we assemble the get link for the direction to our engine "gs.php". 
            var link1 = "http://127.0.0.1/jqm/gs.php?needle=" + $("#subcat_search").val();

            $.ajax({
                url: link1,
                // ajax function is called sending the input string to "gs.php".
                success: function(result){
                    // result is stuffed in the label.
                    $("#search_val").html(result);
                }
            });
        })   

    });
</script>
</head>

<body>

<!-- the input field for search string -->
<input type="text" id="subcat_search">
<br>
<!-- the output field for stuffing the output. -->
<label id="search_val"></label>

</body>
</html>

Now we will include an engine, make a file, call it anything you like. In my case it is gs.php.

$head = "https://maps.googleapis.com/maps/api/place/textsearch/json?query="; //our head
$key = "your key here"; //your key
$hay = $_GET['needle'];

$hay = str_replace(" ", "+", $hay); //replacing the " " with "+" to design it as per the google's requirement 
$kill = $head . $hay . "&key=" . $key; //assembling the string in proper way . 
print file_get_contents($kill);

I have tried to keep the example as simple as possible. And because it executes the link on every keypress, the quota of your API will be consumed pretty fast.

Of course there is no end to things we can do, like putting the data into a table, sending to database and so on.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • Please do not post the same answer more than once. And please be sure your answer actually answers the question. If you need to direct someone elsewhere for more information, it's not a good answer. – elixenide Dec 10 '15 at 20:02
  • Hello I used the link because it has some more information like pre requisites etc . If you feel its in appropriate i will remove it . – Harsh Mehta Dec 11 '15 at 13:14