Search Unity

unity web request unknown error webgl

Discussion in 'Editor & General Support' started by arivazhagan, Feb 25, 2022.

  1. arivazhagan

    arivazhagan

    Joined:
    Jun 6, 2018
    Posts:
    3
    WebRequest works in editor but not working in webgl.

    void Start()
    {
    StartCoroutine(GetRequest("https://www.example.com"));
    }
    IEnumerator GetRequest(string uri)
    {
    using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
    {
    // Request and wait for the desired page.
    yield return webRequest.SendWebRequest();

    string[] pages = uri.Split('/');
    int page = pages.Length - 1;

    switch (webRequest.result)
    {
    case UnityWebRequest.Result.ConnectionError:
    case UnityWebRequest.Result.DataProcessingError:
    Debug.LogError(pages[page] + ": Error: " + webRequest.error);
    break;
    case UnityWebRequest.Result.ProtocolError:
    Debug.LogError(pages[page] + ": HTTP Error: " + webRequest.error);
    break;
    case UnityWebRequest.Result.Success:
    Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
    break;
    }
    }
    }

    this is the code im using it returns www.example.com in editor console and when i host it in itch.io it returns unknown error in the console
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    In WebGL by default you cannot access other sites, only your own. You have to configure CORS on server, to be able to do that.
     
  3. arivazhagan

    arivazhagan

    Joined:
    Jun 6, 2018
    Posts:
    3
    Thanks @Aurimas-Cernius I've found that recently. If u explain it clearly how to do that other people from our community can know.