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

推荐订阅源

S
SegmentFault 最新的问题
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
月光博客
月光博客
IT之家
IT之家
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
小众软件
小众软件
C
Check Point Blog
B
Blog RSS Feed
H
Help Net Security
博客园 - 司徒正美
L
LangChain Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - habin

利用KindEditor实现公司通讯录的维护 在winform嵌入外部应用程序 Devexpress RichEditControl 导入word文件后字体变为方正姚体的解决方案 Js下载文件到本地(兼容多浏览器) 解决lhgDialog插件在IE11浏览器的BUG Devexpress ChartControl 柱状图简单例子 解决“远程会话已断开连接,因为访问被拒绝导致许可证存储的创建失败,请使用提升的权限运行远程桌面客户端”问题 MVC项目初次发布到IIS可能会遇到的问题 Android Studio下载及离线升级方法 动态调用WebService 统计某个单词出现次数 不能在 Page 回调中调用 Response.Redirect 解决方法 JQuery TextExt 控件使用 替换文本框title提示文本 终于解决SQL Server 2008 64位系统无法导入Access/Excel的问题 2012/08/01 Asp.net项目部署ActiveReport jQuery Mobile对话框插件 JS实现日期比较 Apache根目录运行.net网站
通过ashx获取JSON数据的两种方式
habin · 2012-08-02 · via 博客园 - habin

网上asp.net开发jquery mobile的示例很少,根据网上提供的不完全资料自己研究了一下,通过两种方式可以获取JSON数据:

第一,通过get实现:

//前面引用省略
<script>

$('#frmmain').live('pageinit', function(event) {
        $.get('handler/aprvhandler.ashx', { type: 'myParam', cnt: '10' }, function(data) {

            var userlist = $.parseJSON(data); //解析JSON
            $("#userlist").append("<ul data-role='listview'></ul>");  
            $.each(userlist, function(index, value) {

                var temp = "<li><a href='aprvdetail.aspx?id=" + value + "'><h3>" + value + "</h3></a></li>";
                $("#userlist ul").append(temp);
            });
            $("#userList").trigger("create"); 

            });
});
</script>

第二,通过post实现:

//前面引用省略
<script>

   $('#frmmain').live('pageinit', function(event) {
        $.post('handler/aprvhandler.ashx', { type: 'myParam', cnt: '10' }, function(data) {
            $("#userList").append("<ul data-role='listview'></ul>");
            $.each(data, function(index, value) {

                var temp = "<li><a href='aprvdetail.aspx?id=" + value + "'><h3>" + value + "</h3></a></li>";
                $("#userList ul").append(temp);
            });
            $("#userList").trigger("create"); 

            },"json");
    });
</script>

 

//通用页面
<body>
    <!-- Home -->
    <div data-role="page" data-theme="b" id="frmmain"  data-ajax="false">
        <div data-theme="b" data-role="header">
            <h4>
                XXXX系统
            </h4>
        </div>
        <div data-role="content" style="padding: 15px">
            <div data-role="collapsible-set" data-theme="" data-content-theme="">
                <div data-role="collapsible" data-collapsed="false">
                    <h3>
                        用户列表
                    </h3>

                    <div data-role="content"  id="userList" class="content"></div>

                </div>
            </div>

</body>
</html>

两种实现方式差不多,只是$.get方法没有json这个参数,所以在代码里面解析。

 后台ashx也比较简单,需要引用Jayrock.JSON.dll第三方控件

<%@ WebHandler Language="C#" Class="AprvHandler" %>

using System;
using System.Web;
using Jayrock.Json;
using System.IO;

public class AprvHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        HttpContext.Current.Response.ContentType = "text/plain";
        string query = HttpContext.Current.Request.Params["type"];
        if (string.IsNullOrEmpty(query))
        {
            HttpContext.Current.Response.Write("parameter error");
            return;
        }
        string strJsonText = @"{'id':1,'count':'4','name':'Alice','list':[1001,1002,1003,1004]}";
        JsonReader reader = new JsonTextReader(new StringReader(strJsonText));

        JsonObject jsonObj = new JsonObject();
        jsonObj.Import(reader);

        HttpContext.Current.Response.Write(jsonObj);

        HttpContext.Current.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}