Search Unity

No 'Access-Control-Allow-Origin' header

Discussion in 'Web' started by domdev, Jan 31, 2020.

  1. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
    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", "*");
     
  2. alexsuvorov

    alexsuvorov

    Unity Technologies

    Joined:
    Nov 15, 2015
    Posts:
    327
    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:
    Code (config):
    1. 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):
    Code (config):
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <configuration>
    3.     <system.webServer>
    4.         <httpProtocol>
    5.             <customHeaders>
    6.                 <add name="Access-Control-Allow-Origin" value="*" />
    7.             </customHeaders>
    8.         </httpProtocol>
    9.     </system.webServer>
    10. </configuration>
    If you are performing a cross-origin request to a .php resource, you can set this header directly from the PHP code:
    Code (PHP):
    1. <?php
    2.   header("Access-Control-Allow-Origin: *");
    3. ?>
    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:
    Code (config):
    1. Header set Access-Control-Allow-Headers "myheader"
    for IIS:
    Code (config):
    1. <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) :
    Code (PHP):
    1. header("Access-Control-Allow-Headers: myheader");
     
    WaqasGameDev and guneyozsan like this.
  3. domdev

    domdev

    Joined:
    Feb 2, 2015
    Posts:
    375
    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
     
    Last edited: Feb 3, 2020
  4. WaqasGameDev

    WaqasGameDev

    Joined:
    Apr 17, 2020
    Posts:
    118
    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.
     
  5. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    300
    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?
     
  6. radhwane

    radhwane

    Joined:
    Jul 4, 2017
    Posts:
    2
    what about nginx ?
     
    IbrahimRashwan likes this.