Unity3d + WebGL = Cross-Reference Request Blocked

I was wondering if someone could briefly explain how you get the REST api to work with a Unity3D project built for the WebGL platform. I just started changing my project today, thinking I could use REST to get around the use of in-flow sections in a WebGL assembly I need to do. I quickly ran into a CORS problem and am not familiar with it, I'm not sure how to solve the problem.

I am currently using the WWW class to send a request from Unity.

Example "Login" by a user:

        WWWForm form = new WWWForm();

        var headers = form.headers;
        headers["Method"] = "GET";
        headers["X-Parse-Application-Id"] = AppID;
        headers["X-Parse-REST-API-Key"] = RestID;
        headers["X-Parse-Revocable-Session"] = "1";
        headers["Content-Type"] = "application/json";

        WWW www = new WWW("https://api.parse.com/1/login?username="+name+"&password="+password, null, headers);

      

This works fine in the editor, but after creating the WEBGL and uploading to my node in Parse, the following happens:

The following error appears in FireFox:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.parse.com/1/login?username=jleslie5159&password=Osulator5159!. This can be fixed by moving the resource to the same domain or enabling CORS.

      

And something similar in Chrome ...

+3


source to share


2 answers


For someone else, I solved my problem like this:

WWWForm form = new WWWForm();
        var headers = form.headers;
        headers["X-Parse-Application-Id"] = "AppId";
        headers["X-Parse-REST-API-Key"] = "RestKey";
        headers["Content-Type"] = "application/json";
        WWW www = new WWW("https://api.parse.com/1/login?username="+name+"&password="+password, null, headers);
        while(!www.isDone)
            yield return 1;

      



The problem is with setting "headers [" Method "] =" GET "" Apparently only certain headers are allowed to send or you are causing a CORS violation. I resolved the problem by reading the response in the browser console, which states why the request was blocked. And just removed the offensive headlines.

+2


source


I solve by adding the Access-Control-Allow-Origin: * header to the response from the server. For explanation, you can understand it from this link: https://developer.tizen.org/dev-guide/2.2.0/org.tizen.web.appprogramming/html/guide/w3c_guide/sec_guide/cors.htm



Hope this help :)

0


source