• ASP输出JSON数据及客户端jQuery处理方法


    首先ASP处理JSON需要json官方提供的JSON For ASP 封装类文件,下载地址:http://code.google.com/p/aspjson/downloads/list

    下载最新的JSON_2.0.4.asp文件备用。

    1.ASP简单JSON对像及数组输出

    Demo1.asp

    <%@LANGUAGE=”VBSCRIPT” CODEPAGE=”65001″%>
    <% Response.Charset = “UTF-8″ %>
    <% Response.ContentType = “text/JSON” %>
    <!–#include file=”../Inc/JSON_2.0.4.asp”–>
    <%
    Dim Json,Array(4)
    Set Json = jsObject()
    Json(“Title”) = ” ASP输出JSON数据及客户端jQuery 处理方法”
    Json(“Url”) = “#”
    Json(“PubDate”) = Now()
    Array(0) = “QQ空间”
    Array(1)= “腾讯微博”
    Array(2)= “新浪微博”
    Array(3)= “网易微博”
    Array(4)= “搜狐微博”
    Set Json(“Share”) = jsArray() ‘数组对像
    Json(“Share”) = Array ‘ASP数组直接传给JSON数组对像
    Json.Flush
    Set Json = Nothing
    %>

    前台请求页面Demo1.html

    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
    <html xmlns=”http://www.w3.org/1999/xhtml”>
    <head>
    <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
    <title>ASP输出JSON数据及客户端jQuery 处理方法</title>
    <script type=”text/javascript” src=”../Scripts/-1.7.2.min.js”></script>
    <script type=”text/javascript”>
    $(document).ready(function(){
    $(“#btn”).click(function(){
    $.getJSON(“Demo1.asp”,function(data){
    $(“#View”).html(“标题:”+data.Title+”<br />URL:”+data.Url+”<br />时间:”+data.PubDate+”<br />分享:”);
    $.each(data.Share,function(i,n){//遍历数组
    $(“#View”).append(n + ” | “);
    });
    });
    });
    });
    </script>
    </head>

    <body>
    <input type=”button” id=”btn” value=”显示” />
    <div id=”View”></div>
    </body>
    </html>

    是不是很简单呀?

    2.ASP数据库数据输出JSON

    Demo2.asp

    <%@LANGUAGE=”VBSCRIPT” CODEPAGE=”65001″%>
    <% Response.ContentType = “application/json; charset=utf-8″ %>
    <!–#include file=”../Inc/JSON_2.0.4.asp”–>
    <!–#include file=”../Inc/Conn.asp”–>
    <%
    Function QueryToJSON(dbc, sql) ‘此函数来自JSON官方JSON_UTIL_0.1.1.asp
    Dim rs, jsa
    Set rs = dbc.Execute(sql)
    Set jsa = jsArray()
    While Not (rs.EOF Or rs.BOF)
    Set jsa(Null) = jsObject()
    For Each col In rs.Fields
    jsa(Null)(col.Name) = col.Value
    Next
    rs.MoveNext
    Wend
    Set QueryToJSON = jsa
    End Function

    SQLstr = “SELECT U_ID,U_Name,U_Age,U_Sex FROM User LIMIT 0,10″  ’这里使用的是MySQL数据库

    Response.Write QueryToJSON(Conn, SQLstr).Flush
    %>

    前台页面Demo2.html

    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
    <html xmlns=”http://www.w3.org/1999/xhtml”>
    <head>
    <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
    <title>ASP输出JSON数据及客户端jQuery 处理方法</title>
    <script type=”text/javascript” src=”../Scripts/jquery-1.7.2.min.js”></script>
    <script type=”text/javascript”>
    $(document).ready(function(){
    $(“#btn”).click(function(){
    $.getJSON(“Demo2.asp”,function(data){
    $.each(data,function(i,n){
    var $tr = $(“#View”).find(“tr.Demo”).clone();
    $tr.removeClass(“Demo”);
    $tr.find(“td:eq(0)”).text(n.U_ID);//第一个td
    $tr.find(“td:eq(1)”).text(n.U_Name);
    $tr.find(“td:eq(1)”).text(n.U_Name);
    $tr.find(“td:eq(2)”).text(n.U_Age);
    $tr.find(“td:eq(3)”).text(n.U_Sex);
    $(“#View”).append($tr);
    });
    $(“#View”).find(“tr.Demo”).remove();//移除空行
    });
    });
    });
    </script>
    </head>

    <body>
    <input type=”button” id=”btn” value=”显示” />
    <table width=”360″ border=”1″ id=”View”>
    <tr>
    <td align=”center”><strong>ID</strong></td>
    <td align=”center”><strong>姓名</strong></td>
    <td align=”center”><strong>年龄</strong></td>
    <td align=”center”><strong>性别</strong></td>
    </tr>
    <tr class=”Demo”>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </table>
    </body>
    </html>

    ASP利用JSON官方类输出JSON就是这么简单。

    本例在Windows 2008R2+MySQL5.5+ASP环境下测试通过。

    http://code.google.com/p/aspjson/ 有详细的说明和下载,希望本文对您的ASP编程有所帮助。

  • 相关阅读:
    PS总结
    有用的官方API和官网
    获取jQuery DataTables 的checked选中行
    jQuery DataTables 问题:Cannot reinitialise DataTable
    jQuery DataTables 分页
    实现页面跳转和重定向的几种方法
    iframe父页面和子页面高度自适应
    双核浏览器默认选择内核渲染自己开发的网页
    使用母版页的子页面无法显示母版页图片
    gdb安装
  • 原文地址:https://www.cnblogs.com/meetrice/p/4466355.html
Copyright © 2020-2023  润新知