Wednesday, December 19, 2018

unity - WWW request does not connect using webgl platform


I use request like code below:


string url = "http://localhost:51820/Home/login/" + end+"/";
WWW www2 = new WWW(url);

StartCoroutine(waitForLogin(www2));

It works fine in the editor but the html build shows a connection problem. message of www error is only "unknown error"



Answer



When you look into your browsers error console, you will likely notice an error message like this:



Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:51820/Home/login/



For security reasons, web browsers do not allow javascript applications to send http requests to localhost when they aren't also loaded via localhost. Note that loading a html document via the file:/// pseudo-protocol is not considered the same as loading from http://localhost.


What you can do as a workaround is put the game into the document directory of your local webserver and also load it via an URL like http://localhost:51820/yourgame/index.html.



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...