前端js代码
$.ajax({
type: 'post',
url: '/ajax/sysInfo/AddSWsys_Role.ashx',//找不到的话路径可能不对,去掉最前面的/;mvc的话url 为controller中的action如:/Controller/Action; webservice:/webservice.ashx/action
data:'name=abc&psw=aaa',//mvc(data:{arg1:val1,arg2:val2});webservice(data:"{'arg1':'val1'}")
success: function (data) {
if (data == "True") {
alert("添加成功");
}
else if (data == "repeat") {
alert("已存在此名称");
}
else {
alert("添加失败");
}
}
});
后端先建立一个 一般处理程序,即url调用
public void ProcessRequest(HttpContext context)//webservice传的参数直接定义为方法的参数
{
string user_name = context.Request"name"];//获得前端传来的数据
string password = context.Request["psd"];
//string password = context.Request.QueryString["psd"];
//可以连接数据库传数据
//也可以获得数据转换为json传到前端
context.Response.ContentType = "text/plain";
context.Response.Write(user_id);//发回前端的字符串
}