我想知道是否有人可以简要解释如何让 REST api 与构建到 WebGL 平台的 Unity3D 项目一起运行。我今天刚刚开始更改我的项目,认为我可以使用 REST 来解决 Parse 在我需要制作的 WebGL 构建中使用线程的问题。我很快就遇到了 CORS 问题,但由于不熟悉它,我不确定如何解决该问题。
目前,我使用 WWW 类从 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 的我的主机后,会发生以下情况......
我在 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 中也有类似的东西......
对于其他人来说,我解决了我的问题,如下所示:
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 违规。我通过阅读浏览器控制台中的响应解决了该问题,该响应指定了请求被阻止的原因。并且刚刚删除了有问题的标头。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)