Unity WebGL POST请求在构建游戏后不起作用

问题描述 投票:1回答:1

我的统一webGL游戏在构建游戏后似乎没有向服务器发送发布请求,但是在编辑器中工作正常。除此之外,还有其他事情吗?

IEnumerator PostRequestY(string url, string jsonToAPI)

{
    Debug.Log("--------------Game Data Sending --------------");
    var uwr = new UnityWebRequest(url, "POST");
    jsonToAPI = jsonToAPI.Replace("'", "\"");
    //Debug.Log(jsonToAPI);
    byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(jsonToAPI);
    uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
    uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
    uwr.SetRequestHeader("Content-Type", CONTENT_TYPE);
    uwr.SetRequestHeader("x-api-key", X_API_KEY);

    //Send the request then wait here until it returns
    yield return uwr.SendWebRequest();

    if (uwr.isNetworkError)
    {
        Debug.Log("Error While Sending Game Data: " + uwr.error);
        Debug.Log("URL ::: " + url);
        messageText.text = uwr.error;
    }
    else
    {
        //Debug.Log("Game Data Received: " + uwr.downloadHandler.text);
        string recievedMessage = uwr.downloadHandler.text;
        //Debug.Log("Respond for Game Data " + recievedMessage);
        messageText.text = uwr.downloadHandler.text +" & "+ accessToken;
    }
}

此消息在编辑器中玩游戏时出现...

This message appears when the game is played in the editor...

[构建完成后玩游戏时会显示此消息...enter image description here

为什么会这样?任何建议将不胜感激...谢谢

unity3d unity-webgl unitywebrequest
1个回答
0
投票

在Unity WebGL中,基础Web请求使用fetchXMLHttpRequest完成,这意味着您必须在服务器上正确配置CORS。默认情况下,您几乎不能设置任何请求标头。

请参阅https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader以获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.