zoukankan      html  css  js  c++  java
  • json+mvc

     返回JsonResult序列化的Json对象

        
    public class MyController : Controller 

       // 必须返回ActionResult类型 
        public ActionResult HelloWorld() 
        { 
            ViewData["Message"] = "Hello World!"; 
            return View(); 
        } 
     
        public ActionResult Json() 
       { 
         Dictionary<string, object> dic = new Dictionary<string, object>(); 
         dic.Add("id", 100); 
         dic.Add("name", "hello"); 
         return Json(dic, JsonRequestBehavior.AllowGet); 
        }
    }

        注意:需要设置参数,JsonRequestBehavior.AllowGet,
        否则会提示错误:此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。
        若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。

    3、View层 接受页面 使用JQuery 


      
    $.ajax({
              url: "/My/Json",
              type: "POST",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (data) {
                  /*
                  //接受数据
                  当 data 为拼接字符串时,需要下面方法转换成 json对象
                  var json = jQuery.parseJSON(data);
                 */
                   alert(data.id+","+data.name); // data 为json对象
              },
              error: function ErrorCallback(XMLHttpRequest, textStatus, errorThrown) {
                  alert(errorThrown + ":" + textStatus);
              }
          });        

  • 相关阅读:
    Codeforces Global Round 11 E Xum
    【NFLSPC #2】Polynomial
    【SHOI2015】脑洞治疗仪 题解 (线段树)
    CDQ分治与整体二分 学习笔记
    二维树状数组 学习笔记
    博弈论 学习笔记
    【JSOI2007】文本生成器 题解(AC自动机+动态规划)
    【NOI2018】归程 题解(kruskal重构树+最短路)
    【NOI2017】游戏 题解(2-SAT+缩点)
    【BZOJ4398】福慧双修 题解(建图优化)
  • 原文地址:https://www.cnblogs.com/sunzgod/p/4171983.html
Copyright © 2011-2022 走看看