在div标签中显示响应 [英] Show response in the div tag

查看:53
本文介绍了在div标签中显示响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过AJAX调用一个方法,并且实际上正在接收响应,但是我想打印该答案或将其显示在 div 中,但我没有正确地做到这一点.希望我的回复显示在div #result

I'm calling a method by AJAX and I'm actually receiving a response, but I want to print that answer or show it in a div and I'm not doing that correctly.I want my response to be shown in a 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 方法更改为

Firstly you need to change your GetData method to return Json:

[HttpGet]
public ActionResult GetData()
{
    var st = "kyo please help me u.u";
    return Json(new { success = true, message = st }, JsonRequestBehavior.AllowGet);
}

然后您可以在 div 标记中显示响应,如下所示:

And then you can show the response in your div tag like this:

success: function (response) {
    $('#result').text(response.message);
},

这篇关于在div标签中显示响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆