我想在Unity 3d中使用两个json参数用户名和密码调用登录api。
我在stackoverflow上关注了很多帖子。但是我的请求参数不在服务器上。如果我从我的Android应用程序和邮递员和chorome中调用此api,它在那里工作正常。
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Search</span>
<input type="text" name="search_text" id="search_text" placeholder="Search by Customer Details" class="form-control" />
</div>
</div>
<br />
<div id="result"></div>
</div>
<!--div class="content">
<h1>Dashboard</h1>
<p>Here you can do fuck</p>
</div-->
<!--dashboard-->
</body>
</html>
<script>
$(document).ready(function(){
load_data();
function load_data(query)
{
$.ajax({
url:"account-settings-server-A.php",
method:"POST",
data:{query:query},
success:function(data)
{
$('#result').html(data);
}
});
}
$('#search_text').keyup(function(){
var search = $(this).val();
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>
所以我的问题是如何在统一3d中用json请求调用post api。
请帮忙。
答案 0 :(得分:6)
您需要手动设置内容标题和邮件正文,并将表单数据字符串转换为json字符串,并将参数发送到CallLogin
:
public IEnumerator CallLogin(string url, string logindataJsonString)
{
var request = new UnityWebRequest (url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(logindataJsonString);
request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (www.error != null)
{
Debug.Log("Erro: " + www.error);
}
else
{
Debug.Log("All OK");
Debug.Log("Status Code: " + request.responseCode);
}
}