博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
主攻ASP.NET MVC4.0之重生:ASP.NET MVC使用JSONP
阅读量:6998 次
发布时间:2019-06-27

本文共 2767 字,大约阅读时间需要 9 分钟。

原文:

原文地址

1.创建JsonpController

public class JsonpController : Controller    {        // GET: /Jsonp/        VoteUserRepository userrepository = new VoteUserRepository();        [HttpGet]        public JsonpResult GetData(int? page)        {            var list = userrepository.GetModelList().Where(d => d.PhotoWorkInPoll != null).Where(d => d.PhotoWork.FirstOrDefault().State == 1);            var userlist = from c in userrepository.GetPageModelList(list, 8, page ?? 1)                          select new { c.VoteUserID, c.UserName };            JsonpResult result = new JsonpResult(userlist);            return result;        }    }

2.创建JsonpResult

public class JsonpResult : JsonResult    {        object data = null;        public JsonpResult()        {        }        public JsonpResult(object data)        {            this.data = data;        }        public override void ExecuteResult(ControllerContext controllerContext)        {            if (controllerContext != null)            {                HttpResponseBase Response = controllerContext.HttpContext.Response;                HttpRequestBase Request = controllerContext.HttpContext.Request;                string callbackfunction = Request["callback"];                if (string.IsNullOrEmpty(callbackfunction))                {                    throw new Exception("Callback function name must be provided in the request!");                }                Response.ContentType = "application/x-javascript";                if (data != null)                {                    JavaScriptSerializer serializer = new JavaScriptSerializer();                    Response.Write(string.Format("{0}({1});", callbackfunction, serializer.Serialize(data)));                }            }        }    }

Json数据内容地址:

格式例如如下:

JsonCallback([{"VoteUserID":1264,"UserName":"sjc196576           "},{"VoteUserID":1265,"UserName":"竹山县朱本双        "},{"VoteUserID":1266,"UserName":"qwe1725060988       "},{"VoteUserID":1267,"UserName":"堵河1982610         "},{"VoteUserID":1268,"UserName":"625297524           "},{"VoteUserID":1269,"UserName":"chen223150          "},{"VoteUserID":1270,"UserName":"1296909213          "},{"VoteUserID":1271,"UserName":"878223109           "}]); 3.其他页面调用数据方法
function TestCallback () {        $.ajax({             type: "get",             async: false,             url: "http://localhost:12111/Jsonp/GetData?page=1&callback=JsonCallback",             dataType: "jsonp",             jsonp: "callback",             jsonpCallback:"JsonCallback",             success: function(json){                     for (var i=0;i<7;i++){                 alert(json[i].UserName);                }             },             error: function(){                alert('失败');             }         });        }

 

 

转载地址:http://dmdvl.baihongyu.com/

你可能感兴趣的文章
社会好比一张千层饼
查看>>
面向对象的特征有哪些方面
查看>>
uboot中gd的定义和使用
查看>>
Tcpdump MySQL Query
查看>>
读书笔记-APUE第三版-(8)进程控制
查看>>
JSFL 获取当前脚本路径,执行其他脚本
查看>>
理解JavaScript中的事件处理
查看>>
Win7 公布网站 HTTP 错误 404.4 - Not Found
查看>>
特征选择方法之信息增益
查看>>
mac jdbc连接mysql
查看>>
Activity生命周期的学习以及Logcat的使用
查看>>
Longest Valid Parentheses leetcode java
查看>>
Android的两种菜单
查看>>
poj 1218 THE DRUNK JAILER
查看>>
WordPress SEO ☞ WordPress网站终极优化指南
查看>>
Environment 常用方法
查看>>
【TYVJ】1338 QQ农场(最大流+最大权闭合图)
查看>>
解决Python2.7的UnicodeEncodeError: 'ascii' codec can’t encode异常错误
查看>>
最近在准备开发进销存管理系统
查看>>
TCP/IP协议
查看>>