标签:
<script type="text/javascript"> function test() { $.ajax({ url: ‘@Url.Action("GetTestData")‘, type: "POST", success: function (result) { $("#lblMsg").text(result.msg); } }) } </script>
后台选择返回json数据(当然也可以返回string的字符串等等),
return Json(new { msg = "Datetime from server:" + DateTime.Now.ToString("HH:mm:ss"),msgType="ok" }, JsonRequestBehavior.AllowGet);
注意ajax返回成功后调用的参数result调用result.msg是指代后台封装的msg(类似键值对的意思)
mvc中封装了Unobtrusive Ajax,
1.使用Unobtrusive Ajax必须现在web.config文件的节点configuration/appSettings下配置的 UnobtrusiveJavaScriptEnabled 值设为 true
<configuration> <appSettings> ... <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> </configuration>
2.引入jquery.unobtrusive-ajax.min.js文件,一般是在/Views/Shared/_Layout.cshtml引入
标签:
原文地址:http://www.cnblogs.com/ilooking/p/4307513.html