Unity3d + WebGL =跨域请求被阻止 [英] Unity3d + WebGL = Cross-Origin Request Blocked

查看:300
本文介绍了Unity3d + WebGL =跨域请求被阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以简要解释一下如何使REST api与内置于WebGL平台的Unity3D项目一起运行.我今天才开始更改项目,以为我可以使用REST解决我需要制作的WebGL构建中Parse对线程的使用.我很快就遇到了CORS问题,但并不熟悉它,我不确定如何解决该问题.

I was wondering if anyone could briefly explain how you get the REST api to function with Unity3D project built to WebGL platform. I just started changing my project over today thinking I could use REST to get around Parse's use of threading in a WebGL build I need to make. I promptly ran into the CORS problem though and not being familiar with it, I am unsure how to go about fixing the issue.

当前,我利用WWW类从Unity内部发送请求.

Currently I make use of the WWW class to send the request from within Unity.

用户登录"的示例为:

        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);

这在编辑器中工作正常,但是在构建到WEBGL并上传到Parse的主机后,会发生以下情况...

This works fine in the Editor but after building to WEBGL and uploading to my Host at Parse the following happens...

我在FireFox中收到以下错误:

I receive the following error 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.

Chrome浏览器中的类似内容...

And something similar in Chrome...

推荐答案

对于其他任何人,我都是这样解决我的问题的:

For anyone else looking I solved my problem like so:

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;

问题源于设置"headers ["Method"] ="GET".显然,仅允许发送某些标头,或者您触发了CORS违规.我通过在浏览器控制台中读取响应来解决问题,该响应指定了阻止请求的原因.并刚刚删除了令人反感的标题.

The problem stemmed from setting the "headers["Method"] = "GET"" Apparently only certain headers are allowed to be sent or you trigger a CORS violation. I solved the problem by reading the response in the browser console which specified why the request was blocked. And just removed the offending Headers.

这篇关于Unity3d + WebGL =跨域请求被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆