比如传送这样的数组
{ { "build", "http://www.baidu.com" },
{ "analyse", "http://www.sohu.com" },
{ "openbook", "http://www.163.com" }},
可以把这样的数组转换成JSON数据,然后传递到前台.
后台代码:
1 namespace myapp.Controllers 2 { 3 public class HomeController : Controller 4 { 5 public ActionResult Index() 6 { 7 return View(); 8 } 9 10 public ActionResult GetData() 11 { 12 string mystring = "[{ "第一项": "build", "第二项":"http://www.baidu.com" },{ "第一项": "Brett", "第二项":"http://www.sohu.com" },{ "第一项": "openbook", "第二项":"http://www.163.com" }]"; 13 14 return Json(mystring, JsonRequestBehavior.AllowGet); 15 } 16 } 17 }
Index.cshtml 里面:
1 <script type="text/javascript" src="~/myjquery/jquery.min.js"></script> 2 <script type="text/javascript"> 3 function hhh() { 4 $.ajax({ 5 url: "/myapp/Home/GetData", 6 type: "GET", 7 async: false, 8 dataType: "json", 9 error: function () { alert("err"); }, 10 success: function (data) { 11 var json = JSON.parse(data); 12 $.each(json, function (idx, obj) { 13 alert(obj.第一项); 14 alert(obj.第二项); 15 }); 16 } 17 }); 18 } 19 </script>