zoukankan      html  css  js  c++  java
  • asp.net mvc 从后台向前台传送数组或JSON数据

    比如传送这样的数组 

     { { "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>
  • 相关阅读:
    地狱少女....
    火影忍者&nbsp;是时候结束了
    1942
    待查
    什么是傻瓜
    公车艳遇
    初一
    继承
    第四周学习笔记
    查找里程————mooc《面向对象程序设计——Java语言》-(浙大翁凯)第三周编程题
  • 原文地址:https://www.cnblogs.com/lgx5/p/14285956.html
Copyright © 2011-2022 走看看