在.Net中json应用测试整理

1,博文简介

 a)$.post(url,data,callback,type)的应用。

 b)$.ajax({type:,url:,data:,success:,dataType:})的应用。

结合一本处理程序处理。

2,a)$.post()例子

  ~/Default.aspx

复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title></title>
    <script src="script/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        var login = function () {
            var data = { "username": $("#username").val(), "pwd": $("#pwd").val() }

            $.post("Login.ashx", data, function (message) {
                if (message.success) {
                    alert(message.msg);
                } else {
                    alert(message.msg);
                }
            }, "json");
        }
    </script>
</head>
<body>
    <h3>Login</h3>
    Username:<input id="username" name="username" type="text" /><br />
    Userpass:<input id="pwd" name="pwd" type="password" /><br />
    <button type="button" onclick="login()">Submit</button>
   
</body>
</html>
复制代码

 

 3,b)$.ajax()例子

~/Default2.aspx

复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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 runat="server">
    <title></title>
    <script src="script/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        var login = function () {
            var data = { "username": $.trim($("#username").val()), "pwd": $.trim($("#pwd").val()) }

            $.ajax({ type: "GET", url: "Login.ashx", data: data, success: function (message) {
                if (message.success) {
                    //alert(message.msg);
                    location.href = "http://www.baidu.com";
                }
                else {
                    alert(message.msg);
                }
            }, dataType: "json"
            });

            $.ajax({});
        }
    </script>
</head>
<body>
       <h3>Login</h3>
    Username:<input id="username" name="username" type="text" /><br />
    Userpass:<input id="pwd" name="pwd" type="password" /><br />
    <button type="button" onclick="login()">Submit</button>
   
</body>
</html>
复制代码

 

4,c)处理页面(一般处理程序)

 ~/Login.ashx

复制代码
<%@ WebHandler Language="C#" Class="Login" %>

using System;
using System.Web;
using System.Web.Script.Serialization;  //引用,脚本序列化

public class Login : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        message msg = null;
        string username = context.Request["username"];
        string pwd = context.Request["pwd"];
        if (username == "rain" && pwd == "m123")
        {
            msg = new message(true, "S");
        }
        else
        {
            msg = new message(false, "F");
        }

        //创建序列化对象
        JavaScriptSerializer jss = new JavaScriptSerializer();
        context.Response.Write(jss.Serialize(msg)); //返回序列化对象
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
    
    /// <summary>
    /// 返回序列化对象
    /// </summary>
    class message
    {

        public message(bool success, string msg)
        {
            this.success = success;
            this.msg = msg;
        }
        public bool success;
        public string msg;
            
    }

}
复制代码

 

本文转自ylbtech博客园博客,原文链接:http://www.cnblogs.com/ylbtech/archive/2012/10/10/2718044.html,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值