我正在使用 JSON 和 jQuery 将异步发回控制器。
视图中的代码:
@model MvcApplication3.Models.Class1
@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
jQuery(document).ready(function ($)
{
$('.remove').click(function ()
{
alert('Buton Clicked');
$.ajax({
type: "POST",
url: "/Default1Controller/RemoveItem/",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: removeItemCompleted,
error: removeItemFailed
});
});
function removeItemCompleted(results)
{
alert('SUCCESS');
}
function removeItemFailed(request, status, error)
{
alert('failed');
}
});
</script>
<h2>Index</h2>
<input id="Text1" type="text" />
<input id="Text2" type="text" />
<input id="Text3" type="text" />
<input id="Submit1" type="submit" value="submit" class="remove" />
控制器中的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication3.Controllers
{
public class Default1Controller : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult RemoveItem()
{
return Json(new { Status = 1, Message = "Success" });
}
}
}
单击 HTML 按钮时,不会调用控制器事件。
有人知道上面代码中的问题吗?