我有一个按钮,其类为“ btn”,跨度为id“ ajaxtest”。当我点击按钮时,我想在span标签中输入文本“ test”。而这要在ASP.NET MVC中完成。
在视图中,我有以下ajax调用:
<span id="ajaxtest"></span>
<input type="submit" class="btn" name="neverMind" value="Test BTN"/>
<script>
$(document).ready(function () {
$(".btn").click(function () {
$.ajax({
method: "get",
cache: false,
url: "/MyController/MyAction",
dataType: 'string',
success: function (resp) {
$("#ajaxtest").html(resp);
}
});
});
});
</script>
在MyController中,我有以下代码:
public string MyAction()
{
return "test";
}
我知道Ajax的工作原理,也知道MVC的工作原理。我知道错误可能是因为我们在控制器中期望这样的事情:
public ActionResult MyAction()
{
if (Request.IsAjaxRequest())
{
//do something here
}
//do something else here
}
但这实际上是我的问题。我不想在此调用中调用某些局部视图。我只想在我的范围内返回一些字符串,我想知道是否可以在不使用其他局部视图的情况下完成此操作。
我想使用仅返回字符串的简单函数。
将dataType:'string'更改为dataType:'text'
<script>
$(document).ready(function () {
$(".btn").click(function () {
$.ajax({
method: "get",
cache: false,
url: "/login/MyAction",
dataType: 'text',
success: function (resp) {
$("#ajaxtest").html(resp);
}
});
});
});
</script>
我在本地检查它对我有用
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句