如何在 Unity 中使用 json 将数据表单发送到服务器


How to send data form to server using json in unity

我想使用 json 将表单发送到我的服务器这是我的 C# 代码

public string db_url="http://localhost/";
    IEnumerator SaveAllPlayerPrefs(object[] parms)
        {
            string ourPostData = "{'"bone'":'"42'"}";
            Hashtable headers = new Hashtable();
            headers.Add("Content-Type", "application/json");
            headers.Add("Cookie", "Our session cookie");
            byte[] pData = System.Text.Encoding.UTF8.GetBytes(ourPostData);
            WWW webRequest = new WWW(db_url + "SaveAllPlayerPref.php", pData, headers);
            yield return webRequest;
        }

这是我的PHP代码:

<?php
    $sql_connect = mysql_connect("localhost", "root", "") or die ("no DB Connection");
 mysql_select_db("example") or die ("DB not found");
    $bone = $_POST['bone'];  
    mysql_query("INSERT INTO save_game (bone) VALUES ('$bone');");
    mysql_close($sql_connect);
?>
当我运行它时,

这段代码正在运行,但是当我检查数据库时,它没有保存骨骼的值。它将空字符串保存到骨头中,我希望它保存 42.. 它在我的数据库中插入新行,但"骨骼"的值为空在我的数据库中,骨骼是 Varchar(100) 和 utf8_general_ci。

有人可以向我解释一下吗?谢谢之前:)

尝试var_dump($_POST)看看你收到什么。