我正在通过AJAX调用方法,我实际上正在接收回复,但我想打印该答案或在div
中显示它并且我没有正确地做到这一点。我想要我的回复以div #result
<div id="result"></div>
<input type="button" name="name" value="try" onclick="DepListQuery()" />
<script>
function DepListQuery() {
$.ajax({
type: 'GET',
url: '@Url.Action("GetData","Home")',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
},
failure: function (response) {
alert("something get wrong u.u");
}
});
}
</script>
这是我的方法
[HttpGet]
public ActionResult GetData()
{
var st = "kyo please help me u.u";
return Content(st);
}
答案 0 :(得分:1)
首先,您需要将GetData
方法更改为return Json
:
[HttpGet]
public ActionResult GetData()
{
var st = "kyo please help me u.u";
return Json(new { success = true, message = st }, JsonRequestBehavior.AllowGet);
}
然后,您可以在div
标记中显示响应,如下所示:
success: function (response) {
$('#result').text(response.message);
},