Search Unity

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.