No 'Access-Control-Allow-Origin' header

we already add this in the header but still not working, in unity I add it also in uwr.SetRequestHeader

uwr.SetRequestHeader("Access-Control-Allow-Credentials", "true");
uwr.SetRequestHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
uwr.SetRequestHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); uwr.SetRequestHeader("Access-Control-Allow-Origin", "*");


You can see the "No 'Access-Control-Allow-Origin' header is present on the requested resource" error in case when you are performing a cross-domain request and the server does not provide the "Access-Control-Allow-Origin" header in the response. The "Access-Control-Allow-Origin" header should be added to the server response on the server side (and not to the request on the client side). You can use the Network profiler in your browser to check if this response header is present in the server response.

If you are using Apache server, you can add the following ".htaccess" file to the server folder where the requested resource is located:

Header set Access-Control-Allow-Origin "*"

If you are using IIS server, you can add the following "web.config" file to the server folder where the requested resource is located (this can also be achieved using the IIS Manager > HTTP Response Headers menu):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>

If you are performing a cross-origin request to a .php resource, you can set this header directly from the PHP code:

<?php
  header("Access-Control-Allow-Origin: *");
?>

If you need to add custom request headers to your cross-origin request, then the browser will first send a preflighted request, which should be properly handled on the server side. For example, if you would like to add a custom header named "myheader" to your request, then you also need to add the following lines to the server configuration:

for Apache:Header set Access-Control-Allow-Headers "myheader"
for IIS:<add name="Access-Control-Allow-Headers" value="myheader" />
for PHP requests (note that by default OPTIONS requests don't reach PHP handler on IIS, so additional configuration might be required) :header("Access-Control-Allow-Headers: myheader");

2 Likes

he


Hi, thank you for the reply, is this the same with the google cloud?
also I'm sending a Json file then get a response from the server


After struggling for days, Today I read with care that what exactly to do. I was placing code in PHP files and in XML files in server folders but it was not solving the problem. Then today I realised that I am accessing the folder and you mentioned that to set header for a folder, I need to crease a file named .htaccess and place the relevant code there. I did the same and problem is solved. thanks a lot for building.

I tried the htaccess but seems not work properly

Now seems that work with some browsers but not with others....any idea how to solve?

what about nginx ?

1 Like