我正在通过 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);
}
最佳答案
首先,您需要将 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);
},
关于javascript - 在 div 标签中显示响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46531827/