创建一个Ajax控制器
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcValidateDemo.Controllers { public class AjaxController : Controller { // // GET: /Ajax/ public ActionResult Index() { return View(); } public ActionResult GetDate() { return Content(DateTime.Now.ToString()); } } }
Ajax
创建Indx视图
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <script src="~/Scripts/jquery-1.8.2.min.js"></script> <script> $(function () { $("#btnJQ").click(function () { //从后台获取时间 $.ajax( { url: "/Ajax/GetDate", type: "post", success: function (data) { alert(data); }, data: "id=2&name=222" }); //get方式请求 //$.get("Ajax/GetDate", {}, function (data) { // alert(data); //}); }); }); </script> </head> <body> <div> <input type="submit" id="btnJQ" value="获取服务器当前时间"> </div> </body> </html>
Index