下面给出一个$.ajax()方法实现异步请求的例子

$.ajax()方法的参数为一个对象 这个对象可以是很复杂的不过实际使用时 不需要给定那么多的参数

这里给出一个比较简单的但很实用的例子 

前台代码

<!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>
<title>无标题页</title>
<script src="../javascript/jQuery.1.2.6.zh-cn-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">
$(
function(){
$.ajax({
type:
"get",
dataType:
"json",//请求的数据类型 这里可以是"json""xml""html""script""text""jsonp"中的任意一个
url:"/AJAX/Test.ashx",
data:{a:
"wangxiao"},//参数 key/value
success:function(msg){
var data=msg.table;//后台传输过了的对象名为table
$.each(data,function(i,n){//i从0开始n为当前便利的对象
//ToDo
alert(n.a)
})
}
})
})
</script>
</head>
<body>
</body>
</html>

后台代码

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace WXWebTest.AJAX
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Test : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Buffer = true;//缓存输出
#region 禁用浏览器缓存
context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
context.Response.AddHeader("pragma", "no-cache");
context.Response.AddHeader("cache-control", "");
context.Response.CacheControl = "no-cache";
#endregion
string result = "";
string qs = context.Request.Params["a"];//获取参数 可以根据参数进行具体的业务
result = "{\"table\":[{\"a\":\"1\"},{\"a\":\"2\"}]}";//具体的ToDo部分可以在这里
context.Response.Write(result);
}

public bool IsReusable
{
get
{
return false;
}
}
}
}
原文地址:https://www.cnblogs.com/wxzl/p/2287562.html