Unity WebRequest doesn't work on WebGL

Hello, I have trouble with WebRequest on WebGL.
I have the code below.
It is working on Unity editor, but when I build it into WebGL, it doesn’t work.
There is no error running during the Unity editor.
I tested with the forecast API and it did work on WebGL with the same code just changing the URL.

Is it the authority type of problem happening between the code and API?
Do I have to add codes to get the return from the API?

string Best_Move()
    {

        string url = "https://17xn1ovxga.execute-api.ap-northeast-1.amazonaws.com/production/gikou?byoyomi=1&position=sfen ln5+Pl/3s1kg+R1/p2ppl2p/2ps1Bp2/P8/2P3P1P/N2gP4/5KS2/L+r3G1N+b b GS3Pn3p 57";

        StartCoroutine(Get (url));

        IEnumerator Get (string url)
        {
            UnityWebRequest request = new UnityWebRequest();
            request.downloadHandler = new DownloadHandlerBuffer();
            request.url = url;
            request.SetRequestHeader("Content-Type" , "application/json; charset=UTF-8");
            request.method = UnityWebRequest.kHttpVerbGET;

            yield return request.Send();

            if (request.isNetworkError)
            {
                Debug.Log(request.error);
            }
            else
            {
                if (request.responseCode == 200)
                {
                    Debug.Log ("success" + request.responseCode);
                    string bestmove_text = request.downloadHandler.text;

                    int pos = bestmove_text.IndexOf ("bestmove");
                    bestmove = bestmove_text.Substring (pos + 11, 5);

                    if (bestmove.IndexOf("+") == -1)
                    {
                        bestmove = bestmove_text.Substring (pos + 11, 4);
                    }
                }
                else
                {
                    //errorFlag = true;
                    Debug.Log("failed");
                }
            }
        }
        return bestmove;

After this line:

I think you need the coroutine to wait (i.e. yield return null) until the request is complete. For example:

yield return request.Send();

while (!request.isDone)
    yield return null;

Thank you for your advice.
I’ve tried but it was the same.
It worked on the Unity editor but didn’t work when I built into WebGL and uploaded it to the server.

UnityWebRequest.Send() is Obsoluted.
Try UnityWebRequest.SendWebRequest()

yield return request.SendWebRequest()
1 Like

Hello,
Thank you for your advice.
I’ve tried using “.SendWebRequest()” but it did work on the Unity editor, but not on the built to WebGL and uploading to the server.

is it a cross origin request?

Hello!
Thank you very much for the Link.

I understood that it is the cross-origin problem.
When I used the Firefox browser there was an error message like this.

Reason: CORS header ‘Access-Control-Allow-Origin’ is lacking.

So, I’ve inserted the code below.

request.SetRequestHeader(“Access-Control-Allow-Origin” , “*”):

But still, Firefox gives me the same error message.
Is there any other way to clear this error??

Access-Control-Allow-Origin is set by the server side.

Hello, Thank you for your reply.
I searched on Google and understood that Access-Control-Allow-Origin is the setting of the server side.
I’m now asking the creator of the API to set the Access-Control-Allow-Origin.

I’m wondering if there are any ways to clear the error just by adding codes to the client side not fixing the server side.
Are there any ways to do so??

nothing

OK, thanks.