Hello, I can get access to MySQL from PC application. But when I build in WebGL and upload on my website I cannot send SQL query to MySQL I put this file on root of my website: crossdomain.xml Code (CSharp): <?xml version="1.0" encoding="ASCII"?> <cross-domain-policy> <allow-access-from domain="*" secure="false"/> </cross-domain-policy> Thank you for your replay in advance
I will get you more information. My application works when I run from Unity Editor very well. I receive: Ok, we get: Hello But when I upload WebGL application on Google Drive Host it doesn't work. I see empty field. This all my code: Unity side: Code (CSharp): using UnityEngine; using System.Collections; using UnityEngine.UI; public class UIManager : MonoBehaviour { public InputField output; public void SaveStrOnClick() { StartCoroutine("SaveStr"); } IEnumerator SaveStr() { WWWForm form = new WWWForm(); form.AddField("str", "Hello"); WWW www = new WWW("http://dev3dapps.freeoda.com/unity/Polyglot/database.php", form); yield return www; output.text = www.text; } } Server side: Code (CSharp): <?php if (!empty($_POST["str"])) { $str = $_POST["str"]; echo "Ok, we get: ".$str; } else { echo "Error: cannot get str"; }
Thanks! Is it right? I tried it but it doesn't work. I receive nothing: Code (CSharp): <?php header("Access-Control-Allow-Credentials: true"); header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); header('Access-Control-Allow-Origin: *'); if (!empty($_POST["str"])) { $str = $_POST["str"]; echo "Ok, we get: ".$str; } else { echo "Error: cannot get str"; }
I found same problem here: http://answers.unity3d.com/questions/1021309/webgl-build-with-www-post-data-is-blank-on-the-ser.html
It doesn't work too. I receive a blank string: Code (CSharp): using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Collections.Generic; public class UIManager : MonoBehaviour { public InputField output; public void SaveStrOnClick() { StartCoroutine("SaveStr"); } IEnumerator SaveStr() { WWWForm form = new WWWForm(); form.headers.Add("Access-Control-Allow-Credentials", "true"); form.headers.Add("Access-Control-Allow-Headers", "Accept"); form.headers.Add("Access-Control-Allow-Methods", "POST"); form.headers.Add("Access-Control-Allow-Origin", "*"); form.AddField("str", "Hello"); WWW www = new WWW("http://dev3dapps.freeoda.com/unity/Polyglot/database.php", form); yield return www; try { output.text = www.text; } catch (System.Exception) { output.text = "Error in: output.text = www.text;"; } } }
You need to do that on your php file not unity code. The mine work Code (CSharp): header("Access-Control-Allow-Credentials: true"); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time');
Thank you very much! It works now I upload my WebGL application on another free hosting instead of Google Drive Hosting. This is my scripts: Unity side: Code (CSharp): using UnityEngine; using System.Collections; using UnityEngine.UI; public class UIManager : MonoBehaviour { public InputField output; public void SaveStrOnClick() { StartCoroutine("SaveStr"); } IEnumerator SaveStr() { WWWForm form = new WWWForm(); form.AddField("str", "Hello"); WWW www = new WWW("http://dev3dapps.freeoda.com/unity/Polyglot/database.php", form); yield return www; output.text = www.text; } } Server side: Code (CSharp): <?php header("Access-Control-Allow-Credentials: true"); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time'); if (!empty($_POST["str"])) { $str = $_POST["str"]; echo "Ok, we get: ".$str; } else { echo "Error: cannot get str"; }
It still works but the new class should be used. my code c# and php example: //Get method example Code (CSharp): using System.Collections; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; public class ManagerDb : MonoBehaviour { void Start() { StartCoroutine(GetRequest("https://samplepage.000webhostapp.com/file.php")); } IEnumerator GetRequest(string uri) { using(UnityWebRequest webRequest = UnityWebRequest.Get(uri)) { // Request and wait for the desired page. yield return webRequest.SendWebRequest(); if (webRequest.isNetworkError) { Debug.Log(webRequest.error); //or example GetComponent<Text>().text = "Not available."; } else { Debug.Log(webRequest.downloadHandler.text); //Example: component Text from inspector and show the result in game/unity GetComponent<Text>().text = webRequest.downloadHandler.text; } } } } Code (php): <?php header("Access-Control-Allow-Credentials: true"); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); header('Access-Control-Allow-Headers: Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time'); //You get these data from your database $servername = "host"; $username = "123456"; $password = "123456"; $dbname = "databasename"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT * FROM tableName"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while ($row = $result->fetch_assoc()) { echo $row["columName"] . " : " . $row["otherColumname"]; } } else { echo "0 results"; } $conn->close(); You can use the page https://es.000webhost.com/ for free to host its File.php and create your database in the same place, or search for any other repository, I use https: //www.clever-cloud .com / en / for the free database. See more method GET, POST, PUT, DELETE from unity c# here documentacion https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.html