Hi everyone, I’ve been trying to post a json string to an action result on the server for some times now. the problem is that it always sends null (nothing) to the server.
my question is how can I sent data and how should I receive the data On the server?
Here is the code for sending the data:
var encoding = new System.Text.UTF8Encoding();
var header = new Dictionary<string, string>();
header.Add("Content-Type", "text/json");
var www = new WWW(ServerAddress+"/Unity/AutoSave", encoding.GetBytes(Json), header);
yield return null;
while (!www.isDone) { };
Debug.Log(www.text);
yield return www.text;
Something like this works for us. This should hopefully show the relevant parts.
headers = new Dictionary<string, string>
{
{“Accept”, “application/json”},
{“Content-Type”, “application/json; charset=utf-8”}
};
string json = JsonSerializer.ToJson(post_data); // post_data is a class called SavedProject
data = Encoding.UTF8.GetBytes(json);
return new WWW(
url,
data,
headers);
On the receiving end in the controller we have an action like:
[HttpPost]
[Route(“save”)]
public void SaveProject([FromBody] SavedProject project)
{…}
I am also a bit suspicous about the while(!www.isDone){}; in your code. I often found that type of code to get stuck in an infinite loop in WebGL builds.
I believe so yes. In WebGL you are running single threaded so this will deadlock. Using a coroutine should work instead and you could do something like:
while()
yield return null;
unfortunately I couldn’t solve the freeze problem.
I tried having the yield return null inside the while, no luck.
I tried removing the while entirely , no luck (had its own set of problems).