惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Palo Alto Networks Blog
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
量子位
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
腾讯CDC
博客园 - Franky
The Cloudflare Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hugging Face - Blog
Hugging Face - Blog
H
Heimdal Security Blog
L
LangChain Blog
V
V2EX
Jina AI
Jina AI
美团技术团队
V2EX - 技术
V2EX - 技术
V
Visual Studio Blog
Google Online Security Blog
Google Online Security Blog
人人都是产品经理
人人都是产品经理
Security Archives - TechRepublic
Security Archives - TechRepublic
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Know Your Adversary
Know Your Adversary
Hacker News: Ask HN
Hacker News: Ask HN
Apple Machine Learning Research
Apple Machine Learning Research
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
C
Cisco Blogs
博客园 - 三生石上(FineUI控件)
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog
PCI Perspectives
PCI Perspectives
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
B
Blog RSS Feed
N
News and Events Feed by Topic
L
LINUX DO - 热门话题
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threatpost
月光博客
月光博客
Recent Announcements
Recent Announcements
D
DataBreaches.Net
IT之家
IT之家
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 天天无用

ubuntu笔记 修复MVC3中使用Remote验证的一点小问题 诺基亚手机S60系统证书申请、软件签名图文教程 JAXER留言板-一个html页面的ajax留言版 - 天天无用 - 博客园 初探JAXER Jquery插件研究:Ajax File Upload 本月wii游戏太多,感觉有点堕落 使用jQuery加DIV实现可以动态添加的金字塔结构 自制的操作下拉列表框(SELECT)的三个jquery插件(ajax填充、联动、增加选项) - 天天无用 - 博客园 Asp.net 2.0 TreeView控件使用jQuery无刷新添加节点详细说明 用jQuery实现.net 2.0 treeview客户端无刷新操作的实例 基于jQuery的AJAX和JSON实现纯html数据模板 自己写的DataTable转换成JSON字符串的函数 自己写的jQuery自动完成的插件(AutoComplete) asp.net treeview控件无刷新选择和删除节点(使用jquery) - 天天无用 - 博客园 纵向合并gridview单元格的两种方法 .net下两种json序列化速度比对 asp.net 2.0 中 TreeView控件中的checkbox客户端操作 asp.net 2.0中根据roles显示不同的sitemap - 天天无用 - 博客园
jsonp原理实例及mvc中的应用
天天无用 · 2011-05-04 · via 博客园 - 天天无用

最近用的工作用到了jsonp,记录一下

jsonp

简单的原理就是通过<script>标记引入一个定义好json数据的js,因为<script>标记是不受域名限制的


例子
<script type=text/javascript>
function callback(jsondata)
{
    alert(jsondata.UserName);
}
</script>
<script type=text/javascript>
callback({"UserName":"test","Age":11});
</script>
这个应该都能看懂,如果把
callback({"UserName":"test","Age":11});
保存成一个js文件,随便放到那个什么地方,然后这里直接引入这个js,也是能够执行的

jquery已经把这个过程都包装好,直接调用就可以得到json数据

mvc中调用

先定义好一个JsonpResult,

        public class JsonpResult<T> : ActionResult
        {
            public T Obj { get; set; }
            public string CallbackName { get; set; }

            public JsonpResult(T obj, string callback)
            {
                this.Obj = obj;
                this.CallbackName = callback;
            }

            public override void ExecuteResult(ControllerContext context)
            {
                var js = new System.Web.Script.Serialization.JavaScriptSerializer();
                var jsonp = this.CallbackName + "(" + js.Serialize(this.Obj) + ")";

                context.HttpContext.Response.ContentType = "application/json";
                context.HttpContext.Response.Write(jsonp);
            }
        }

Action写法

        public ActionResult GetJsonP(string callback)
        {
            if (string.IsNullOrEmpty(callback)) callback = "callback";
            User u=new User();
            //生成u对象
            return new JsonpResult<InpaiUser>(u, callback);
        }

action返回的就是一段js代码

callback({"UserId":45516,"UserName":"xxx"})
jquery调用
    function getUserInfo(checkUser) {
        $.ajax({
            type: "GET",
            url: "http://xxx/user/getjsonp?callback=?",
            cache: false,
            error: function () {
            },
            jsonp: "callback",
            dataType: "jsonp",
            success: function (result) {
                checkUser(result);//checkUser是一个处理user对象的function
            }
        });
    }