JavaScript的ajax通信实例代码 求

2025-02-24 07:06:34
推荐回答(1个)
回答1:

jquery 的ajax:
$.ajax({
url: "Handler.ashx",
data: { type: 'DelNews', id: id },
success: function (result) {
alert("删除成功!");
location.replace(location.href);
}
});

服务端 handler.ashx中的方法, 注意参数type:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace xx网站后台
{
///


/// Handler 的摘要说明
///

public class Handler : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
var type = context.Request["type"];
switch (type)
{
case "DelNews":
删除新闻(context);
break;
default:
break;
}
}

public void 删除新闻(HttpContext context)
{
string id = context.Request["id"];
id = id.Substring(0, id.LastIndexOf(','));
var t = id.Split(',');
foreach (var item in t)
{
News.删除新闻(item);
}
context.Response.Write("");
}

public bool IsReusable
{
get
{
return false;
}
}
}
}