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:
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:
<?php
if (!empty($_POST["str"]))
{
$str = $_POST["str"];
echo "Ok, we get: ".$str;
}
else
{
echo "Error: cannot get str";
}
I upload my WebGL application on another free hosting instead of Google Drive Hosting.
This is my scripts:
Unity side:
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:
<?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
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;
}
}
}
}
<?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.