转载

MVC,jquery异步

创建一个Ajax控制器

1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Web;  5 using System.Web.Mvc;  6   7 namespace MvcValidateDemo.Controllers  8 {  9     public class AjaxController : Controller 10     { 11         // 12         // GET: /Ajax/ 13  14         public ActionResult Index() 15         { 16             return View(); 17         } 18         public ActionResult GetDate() 19         { 20             return Content(DateTime.Now.ToString()); 21         } 22     } 23  24 }  Ajax

创建Indx视图

1 @{  2     Layout = null;  3 }  4   5 <!DOCTYPE html>  6   7 <html>  8 <head>  9     <meta name="viewport" content="width=device-width" /> 10     <title>Index</title> 11     <script src="~/Scripts/jquery-1.8.2.min.js"></script> 12     <script> 13         $(function () { 14             $("#btnJQ").click(function () { 15                 //从后台获取时间 16                 $.ajax( 17                     { 18                         url: "/Ajax/GetDate", 19                         type: "post", 20                         success: function (data) { 21                             alert(data); 22                         }, 23                         data: "id=2&name=222" 24                     }); 25  26                 //get方式请求 27                 //$.get("Ajax/GetDate", {}, function (data) { 28                 //    alert(data); 29                 //}); 30  31  32             }); 33  34         }); 35     </script> 36 </head> 37 <body> 38     <div> 39         <input type="submit" id="btnJQ" value="获取服务器当前时间"> 40     </div> 41 </body> 42 </html>  Index
正文到此结束
Loading...