So I've been trying to connect a Unity project with an API and build it on WebGl platform. On Android, Windows App and WebGl IN Unity, it works fine, but when I build it on the WebGl platform it doesn't work. Check the API link below for testing: http://syriatech07-001-site1.htempurl.com/Users
The solution I've found but I didn't know how to apply it was to add a HOST to the headers. I've tried this API "https://httpbin.org/get" and it worked very fine. If any additional information needed please let me know.
Edit:
Here's the connection code:
IEnumerator GetDataCoroutineASync()
{
// Showing the result
outPutArea.text = "Loading.....";
string uri = "http://syriatech07-001-site1.htempurl.com/Users";
using (UnityWebRequest request = UnityWebRequest.Get(uri))
{
request.SetRequestHeader("Host", "syriatech07-001-site1.htempurl.com");
request.SetRequestHeader("Access-Control-Allow-Methods", "*");
request.SetRequestHeader("Access-Control-Allow-Headers", "append,delete,entries,foreach,get,has,keys,set,values,Authorization,Origin, Content-Type, X-Auth-Token,Accept, X-Requested-With, Access-Control-Request-Method, Access-Control-Request-Headers");
request.SetRequestHeader("Access-Control-Allow-Origin", "*");
var op = request.SendWebRequest();
yield return op;
if (request.isNetworkError)
{ outPutArea.text = "isNetworkError"; }
else if (request.isHttpError)
{ outPutArea.text = "isHttpError"; }
else
{ outPutArea.text = request.downloadHandler.text; }
}
}
Access to fetch at 'http://syriatech07-001-site1.htempurl.com/Users' from origin 'http://localhost:64929' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. syriatech07-001-site1.htempurl.com/Users:1 Failed to load resource: net::ERR_FAILED
CodePudding user response:
You're using asp core minimal API, add this to your web.config and it'll work.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
In addition, delete all the headers you have added.
request.SetRequestHeader
Check this link from SmarterAsp for more information