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);
}
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);
}
And then you can show the response in your div
tag like this:
success: function (response) {
$('#result').text(response.message);
},