【发布时间】:2020-03-26 07:11:02
【问题描述】:
这是我的 API 请求
public IEnumerator Login(string bodyJsonString)
{
Debug.Log(bodyJsonString);
UnityWebRequest req = UnityWebRequest.Post("localhost:3000/login", bodyJsonString);
req.SetRequestHeader("content-type", "application/json");
yield return req.SendWebRequest();
if (req.isNetworkError || req.isHttpError)
{
Debug.Log(req.error);
}
else
{
Debug.Log("Form upload complete!");
}
}
它返回一个错误状态码 500 并且在服务器上返回一个错误 Unexpected token % in JSON at position 0","severity
这是我的协程调用
public void submitLogin()
{
_username = userInputField.GetComponent<InputField>().text;
_password = passwordInputField.GetComponent<InputField>().text;
Debug.Log("username" + _username);
Debug.Log("password" + _password);
string body = "{'username':'" + _username + "','password','" + _password + "'}";
//API Call
authChexi = new Auth();
StartCoroutine(authChexi.Login(body));
}
如果您对如何处理我的表单主体有任何想法,请告诉我。谢谢
【问题讨论】:
-
我也希望你能以某种方式逃避这个......如果用户输入
'或}作为用户名或密码的一部分怎么办? -
为什么要手动构建 JSON 字符串?这几乎肯定是你的问题所在。使用 Unity 的内置 JSON 序列化程序来代替。
标签: c# json unity3d unity-webgl unitywebrequest