Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Resolved I want to make a request to external PHP with WebGL.

Discussion in 'Multiplayer' started by unitsume, May 4, 2023.

  1. unitsume

    unitsume

    Joined:
    Oct 7, 2022
    Posts:
    31
    Hello.

    I need help.
    I want to make a request to external PHP with WebGL.
    But I got an error.

    I have seen this manual.
    But I don't know how to add the header information.
    What kind of code can add the header?

    Thank you.

    Code (CSharp):
    1. private IEnumerator TestMethod()
    2. {
    3.     string url = "http://xxxx.xxx.xxx.xxx:80/xxx.php?hoge=piyo";
    4.     WWW fuga = new WWW(url);
    5.  
    6.     yield return fuga;
    7.     HelloString(fuga.text); // Javascript window.alert()
    8. }
    Error message

    localhost/:1
    Access to fetch at 'http://xxxx.xxx.xxx.xxx:80/xxx.php?hoge=piyo' from origin 'http://localhost:54658' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
     
  2. This class is long obsolete.
    Use the UnityWebRequest. And if you use it, you can set custom headers through the SetRequestHeader method.
     
  3. unitsume

    unitsume

    Joined:
    Oct 7, 2022
    Posts:
    31
    thank you.
    I was able to deal with it by adding a header on the server side as per the manual.
    I'll also try UnityWebRequest.