您的位置:首页 > 其它

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

2011-12-22 11:26 274 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐