Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS request not working in Safari

I am making a CORS xhr request. This works fine in chrome, however when I run in safari I get an 'Can not load ---- access not allowed by Access-control-allow-origin'. The code is exactly the same and I have set the CORS on the server. Below is my code.(has access control, but you are free to try without the accessToken)

 var water;  var req = new XMLHttpRequest;  req.overrideMimeType("application/json");  req.open('GET', 'https://storage.googleapis.com/fflog/135172watersupplies_json', true);  req.setRequestHeader('Authorization', 'Bearer ' + accessToken);  origThis = this;  var target = this;  req.onload = function() {  water = req;   req.send(null); 

After looking at the request headers I see that a OPTIONS request is made first and this is the request that is not allowed. The origin header is not included in the response in Safari, but is in chrome. What would cause this. Any help would be greatly appreciated.

UPDATE: I have tried in Safari for Windows and it works, so I'm not sure what is going on here. The mac that I am using is a remote access (Macincloud.com), but I don't think that would have anything to do with it.

like image 460
Patrick Avatar asked May 29 '13 22:05

Patrick


People also ask

How do I enable CORS on Safari?

The easiest and most reliable way to CORS in Safari is to disable CORS in the develop menu. Enable the develop menu by going to Preferences > Advanced.

How do I bypass Safari CORS?

Later versions of Safari allow you to Disable Cross-Origin Restrictions. Just enable the developer menu from Preferences >> Advanced, and select "Disable Cross-Origin Restrictions" from the develop menu.

How do I fix the CORS problem in my browser?

Cross-Origin Resource Sharing (CORS) errors occur when a server doesn't return the HTTP headers required by the CORS standard. To resolve a CORS error from an API Gateway REST API or HTTP API, you must reconfigure the API to meet the CORS standard.

How do I fix invalid CORS request?

Go to https://www.getpostman.com/docs/capture in your chrome browser. Click on interceptor extension and then choose add to chrome. Once it is added there is a new icon top right of both the browser and postman that looks like a traffic light. In postman click this and it turns green.


2 Answers

I encountered the same error when making an XHR request against a file in Amazon S3. On Safari 7 it was failing. I know you're not using Amazon S3, but I thought I'd post in case this solution helped others.

The problem was that Safari 7 set the Access-Control-Request-Headers header to "origin, x-requested-with", but my AWS CORS configuration only allowed "x-requested-with":

<?xml version="1.0" encoding="UTF-8"?> <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">     <CORSRule>         <AllowedOrigin>*</AllowedOrigin>         <AllowedMethod>GET</AllowedMethod>         <MaxAgeSeconds>3000</MaxAgeSeconds>         <AllowedHeader>Authorization</AllowedHeader>         <AllowedHeader>x-requested-with</AllowedHeader>     </CORSRule> </CORSConfiguration> 

I added "origin" as an allowed header and everything worked fine.

        <AllowedHeader>origin</AllowedHeader> 

Note: the AllowedOrigin of * is for development purposes only. See @andes comment below for more information.

like image 129
Seth Avatar answered Sep 18 '22 15:09

Seth


I just had a similar problem, CORS error. It would work in Firefox & Chrome but not Safari 10.

Turned out we needed to put the trailing slash on the JSON URL.

like image 24
William Macdonald Avatar answered Sep 19 '22 15:09

William Macdonald