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
<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
相关文章推荐
- 【AJAX】Ajax-向服务器发送请求何时用Get/Post
- Vue------第五天(Vue的Ajax请求,使用Axios,目前熟悉了一下,主要包括GET请求、POST请求、并发请求、请求参数的配置、服务器响应的结构以及对服务器响应错误的简单处理)
- AJAX发送GET、POST、DELETE、PUT请求到服务器
- echarts通过ajax向服务器发送post请求,servlet从数据库读取数据并返回前端
- Node.js响应Ajax的POST请求并保存为JSON文件
- echarts通过ajax向服务器发送post请求,servlet从数据库读取数据并返回前端
- 使用jQuery的ajax方法向服务器发出get和post请求的方法
- Node.js如何响应Ajax的POST请求并且保存为JSON文件详解
- Ajax请求GET/POST方法的封装
- Ajax---通过JSON与服务器通信(发送请求和处理响应)
- 我的Android进阶之旅------>Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- Ajax异步请求——接受服务器返回的五种响应+补充
- js的Ajax的POST请求和servlet文本响应的交互
- 利用Ajax发送HEAD请求读取服务器响应头部
- jquery之利用ajax与服务器交谈(发起GET和POST请求之利用jquery获取数据)
- Ajax的GET与POST方式向服务器发送请求
- Yaws(erlang web服务器框架) 学习记录之获取GET/POST请求参数
- Apache、IIS、Nginx等绝大多数web服务器,都不允许静态文件响应POST请求,否则会返回“HTTP/1.1 405 Method not allowed”错误。
- Ajax技术之与服务器通信-发送请求与处理响应