Multiple Access-Control-Allow-Origin domains

Asked

Viewed 629 times

4

I have a page that serves some data in JSON format based on the data received from the database, but I need to free access so that the mobile version can also obtain such data, as I can put more than one domain in htaccess’s Access-Control-Allow-Origin?

I tried the code below but it didn’t work

<IfModule mod_headers.c>
    SetEnvIf Origin "http(s)?://(www\.)?(dominio.com|m.dominio.com)$" AccessControlAllowOrigin=$0
    Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
</IfModule>
  • None of the answers answers your question?

  • At the moment, no

2 answers

2

Try this:

<FilesMatch "\.json$">
    <IfModule mod_headers.c>
        SetEnvIf Origin "http(s)?://(www\.)?(dominio.com|m.dominio.com)$" AccessControlAllowOrigin=$0
        Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
    </IfModule>
</FilesMatch>

Updated:

Or try to use the addHeader,

response.addHeader("Access-Control-Allow-Origin", "*");

* serves to give access to all domains

To give access to specific domains use:

response.addHeader("Access-Control-Allow-Origin", "http://www.example.com");

  • The error persists No 'Access-Control-Allow-Origin' header is present on the requested Resource

  • Take a look at this question and see if it helps you http://answall.com/questions/86342/cors-no-access-control-allow-origin-header-is-present-on-the-requested-resou

  • The solution presented to htaccess in this question asks me to free up access for anyone using *, and I would not like to use such a method.

  • I showed an example to manually free access to some domains.

  • Just put addHeader inside If mod.headers? And add more lines according to the domains?

  • Header set Access-Control-Allow-Origin "http://www.example.com"

  • Using this way it is only possible to add 1 domain

Show 2 more comments

-2

Maybe that’s not the answer you want, but wouldn’t it be possible to use JSONP? Maybe it’s simpler than trying to touch htacess for different cases.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.