我把处女献给了你

——别误会,本人的博客园处女作。呵呵...

我把自己写的一个客户端Ajax辅助类与大家共享。体积小巧、简单易用、便于理解。

//Ajax辅助类。
function AjaxHelper()
{
       this.OnResponse="";//服务端回传处理
       this.OnError=alert;//错误信息处理
       this.__staticParams__="";
       this.__params__="";
       this.__xmlHttp__=__createXMLHttpRequest__();
}
//添加Post提交时的参数。name和value会被自动编码;isStatic为false时(默认情况),下次调用Post方法之后参数将被清除。
AjaxHelper.prototype.AddParam=function(name,value,isStatic)
{
       var param=encodeURIComponent(name)+"="+encodeURIComponent(value);
       if(isStatic) this.__staticParams__+=(this.__staticParams__==""?"":"&")+param;
       else this.__params__+=(this.__params__==""?"":"&")+param;
}
//Post提交。txtOrXml为"xml"时将回传一个xml文档,默认为回传文本。
AjaxHelper.prototype.Post=function(url,txtOrXml)
{
       var p=this;
       p.__xmlHttp__.onreadystatechange=function()
       {
               if(p.__xmlHttp__.readyState==4)//javascript中ActiveX对象使用this可能出现不可预期情况
               {
                     if(p.__xmlHttp__.status==200)
                     {
                            if(p.OnResponse=="")return;
                            if(txtOrXml=="xml")p.OnResponse(p.__xmlHttp__.responseXML);
                            else p.OnResponse(p.__xmlHttp__.responseText);
                     }
                     else{ p.OnError(p.__xmlHttp__.statusText); }

              }
       }
       p.__xmlHttp__.open("POST",url,true);
       p.__xmlHttp__.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
       p.__xmlHttp__.send(p.__params__+(p.__params__==""?"":"&")+p.__staticParams__);
       p.__params__="";
}
function __createXMLHttpRequest__()
{
       if (window.ActiveXObject)return new ActiveXObject("Microsoft.XMLHTTP");
       else if(XMLHttpRequest)return new XMLHttpRequest();
       else throw new Error("your browser does not support XMLHttpRequest");
}

 

 

     用法举例: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 type="text/javascript" src="Js/AjaxHelper.js"></script>
    <script type="text/javascript">
  function TestUser()
  {
   var sUserName=document.getElementById("tbxUserName").value;
   if(sUserName==""){alert("不能为空");return;}
   
   var oAjax=new AjaxHelper();
   oAjax.AddParam("UserName",sUserName);
   oAjax.OnResponse=TestUser_display;
   oAjax.Post("IsUserExisted.ashx");
  }
  function TestUser_display(txt)
  {
   if(txt=="user existed")alert("× 该用户名已经存在");
   else alert("该用户名可以使用");
  }
    </script>
</head>
<body>
    <form id="form1" runat="server">

     已存在用户名:abc,123,cwx,lxg<br />
     用户名:<asp:TextBox ID="tbxUserName" runat="server"></asp:TextBox><input type="button" value="测试是否存在" οnclick="TestUser()" />
    </form>
</body>
</html>

————————————————————————————————————————

     HttpHandler文件 IsUserExisted.ashx

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

using System;
using System.Web;

public class IsUserExisted : IHttpHandler
{

 public void ProcessRequest(HttpContext context)
 {
  context.Response.ContentType = "text/plain";
  string strUserName = context.Request["UserName"].ToString();
  if ("-abc-123-cwx-lxg-".LastIndexOf("-" + strUserName + "-") != -1)//如果传来的用户名为abc、123、cwx或lxg其中之一
  {
   context.Response.Write("user existed");
  }
  else context.Response.Write("ok");
 }

 public bool IsReusable
 {
  get{return false;}
 }

}

转载于:https://www.cnblogs.com/chenwx/archive/2009/03/14/1411181.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值