在asp.net mvc中对控制器进行简单的Ajax调用
我正在尝试开始使用ASP.NET MVC Ajax调用。
控制器:
public class AjaxTestController : Controller{
//
// GET: /AjaxTest/
public ActionResult Index()
{
return View();
}
public ActionResult FirstAjax()
{
return Json("chamara", JsonRequestBehavior.AllowGet);
} }视图:
<head runat="server">
<title>FirstAjax</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var serviceURL = '/AjaxTest/FirstAjax';
$.ajax({
type: "POST",
url: serviceURL,
data: param = "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFunc,
error: errorFunc });
function successFunc(data, status) {
alert(data);
}
function errorFunc() {
alert('error');
}
});
</script></head>我只需要使用控制器方法返回数据来打印警报。上面的代码只是在我的视图上打印“chamara”。警报未触发。
UPDATE
我修改了我的控制器如下,它开始工作。我不清楚它为什么现在正在工作。有人请解释一下。参数“a”没有关联我添加它因为我不能添加两个方法具有相同的方法名称和参数。我认为这可能不是解决方案,但它的工作
public class AjaxTestController : Controller
{
//
// GET: /AjaxTest/
[HttpGet]
public ActionResult FirstAjax()
{
return View();
}
[HttpPost]
public ActionResult FirstAjax(string a)
{
return Json("chamara", JsonRequestBehavior.AllowGet);
}
}
慕尼黑的夜晚无繁华
饮歌长啸
qq_遁去的一_1
白板的微信
随时随地看视频慕课网APP