在div标签中显示响应

时间:2017-10-02 18:50:30

标签: javascript c# asp.net ajax asp.net-mvc

我正在通过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);
}

1 个答案:

答案 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);
},