Ajax的get/post请求服务器响应

 

Ajax的get/post方式http请求代码

<SCRIPT type=text/javascript>

var myjaxobj=new MyJax();

function MyJax()

{

      this.xmlhttp;

      owner=this;

      if(window.ActiveXObject)

      {    

         //ie浏览器

         try

         {

             this.xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");

         }catch(e)

         {

             this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

         } 

      }else

      {

            //其它浏览器

          this.xmlhttp=new XMLHttpRequest();

      }

    

       this.callback=function ()

       {

           if(owner.xmlhttp.readyState==4)

           {

              if(owner.xmlhttp.status==200)

              {

          

                  

                    Alert(owner.xmlhttp.responseText);

               }

           }

       }

        //get 请求

       this.Sendd=function(url,para)

       {

        

         url=url+"?cardid="+para;

         this.xmlhttp.open("get",url,false);// true为异步请求 false 为同步请求

         this.xmlhttp.onreadystatechange=this.callback;

         this.xmlhttp.send(null);

       }

       

       //post 请求

      /* this.Sendd=function(url,para)

       {

        

         this.xmlhttp.open("post",url,false);// true为异步请求 false 为同步请求

         this.xmlhttp.onreadystatechange=this.callback;

         this.xmlhttp.send(“这是post请求方式”);

       }*/

}

</SCRIPT>

1.服务器端响应AJAX的同步请求,添加一个类继承IhttpHandler接口

public class server:IHttpHandler

{

       public server()

       {     }

 

    #region IHttpHandler 成员

 

    bool IHttpHandler.IsReusable

    {

        get { return true; }

    }

 

    void IHttpHandler.ProcessRequest(HttpContext context)

{

     //获取get请求方式提交的cardid参数

         string id= context.Request["cardid"];

         //设置客户端不缓存,每次都请求服务器

         context.Response.CacheControl = "no-cache";

          //返回的客户端信息

         context.Response.Write(“测试通过!”);

 

        //post请求方式的处理

//Stream se= context.Request.InputStream;

      //  StreamReader streamreader = new StreamReader(se);

       // String  str = streamreader.ReadToEnd();

      //  se.Close();

      //  streamreader.Close();

      //  context.Response.Write(str);

      

    }

 

    #endregion

}

 

Web.config 还要配置如下

              <httpHandlers>

                     <add verb="*" path="*.cs" type="server"/>

              </httpHandlers>

 

2. 服务器端响应AJAX的异步请求,添加一个类继承IhttpAsyncHandler接口

public class server : IHttpAsyncHandler

{

    public server()

    {

        //

        //TODO: 在此处添加构造函数逻辑

        //

    }

 

    public bool IsReusable { get { return false; } }

 

 

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)

    {

        context.Response.Write("<p>Begin IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p>\r\n");

        AsynchOperation asynch = new AsynchOperation(cb, context, extraData);

        asynch.StartAsyncWork();

        return asynch;

    }

 

    public void EndProcessRequest(IAsyncResult result)

    {

    }

 

    public void ProcessRequest(HttpContext context)

    {

        throw new InvalidOperationException();

    }

}

 

class AsynchOperation : IAsyncResult

{

    private bool _completed;

    private Object _state;

    private AsyncCallback _callback;

    private HttpContext _context;

 

    bool IAsyncResult.IsCompleted { get { return _completed; } }

    WaitHandle IAsyncResult.AsyncWaitHandle { get { return null; } }

    Object IAsyncResult.AsyncState { get { return _state; } }

    bool IAsyncResult.CompletedSynchronously { get { return false; } }

 

    public AsynchOperation(AsyncCallback callback, HttpContext context, Object state)

    {

        _callback = callback;

        _context = context;

        _state = state;

        _completed = false;

    }

 

    public void StartAsyncWork()

    {

        ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncTask), null);

    }

 

    private void StartAsyncTask(Object workItemState)

    {

 

        _context.Response.Write("<p>Completion IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p>\r\n");

 

        _context.Response.Write("Hello World from Async Handler!");

        _completed = true;

        _callback(this);

    }

}

 

 

 

参考文献:http://msdn.microsoft.com/zh-cn/library/system.web.httpcontext.aspx

http://msdn.microsoft.com/zh-cn/library/system.web.httpcontext.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值