//$.ajax的post方式
function CommentAll() {
$.ajax({
url: "/ashx/myzhuye/Detail.ashx",
type: "post",
contentType: "application/x-www-form-urlencoded; ", //默认,表单提交格式
dataType: "json",
data: { methodName: "GetAllComment", str1: "atr1", "str2": "str2" },
success: function (data) {
$.each(data,
function (i, v) {
alert(v);
});
}
});
}
private void GetAllComment(HttpContext context)
{
//Params可以取得get与post方式传递过来的值。
string methodName = context.Request.Params["methodName"];
//Form只能取得post方式传递过来的值。
string str1 = context.Request.Form["str1"];
//取得httpRequest传来的值,包括get与post方式
string str2 = context.Request["str2"];
List<string> comments = new List<string>();
comments.Add(methodName);
comments.Add(str1);
comments.Add(str2);
//ajax接受的是json类型,需要把返回的数据转为json格式,要是不转的话只能返回text格式,前台datatype要改为text.
string commentsJson = new JavaScriptSerializer().Serialize(comments);
context.Response.Write(commentsJson);
}
//$.ajax的get方式 (后台还是GetAllComment()方法)
function CommentAll() {
$.ajax({
url: "/ashx/myzhuye/Detail.ashx",
type: "get",
contentType: "application/json; ", //get方式下jsonn格式也可以
dataType: "json",
data: { methodName: "GetAllComment", str1: "atr1", "str2": "str2" },
success: function (data) {
$.each(data,
function (i, v) {
alert(v);
});
}
});
}
//$.get请求方式
//该请求只能返回text数据类型,不能返回数组、json等
//$.get(url,[data],[callback])
function CommentAll() {
$.get("/ashx/myzhuye/Detail.ashx",
{ methodName: "GetAllComment", str1: "atr1", "str2": "str2" },
function(da) {
alert(da);
});
}
//后台还是GetAllComment()方法,需要把返回的类型改为字符串
//$.getJSON方式,跟get唯一的差别是返回的是json格式
//$.getJSON(url,[data],[callback])
function CommentAll() {
$.getJSON("/ashx/myzhuye/Detail.ashx",
{ methodName: "GetAllComment", str1: "atr1", "str2": "str2" },
function (da) {
$.each(da,function(i, v) {
alert(v);
})
});
}
//$.post(url,[data],[callback],[type]),
//$.post请求方式,跟$get差不多,返回的也是字符串,只是多了个type可设置属性,可设为html,xml,json等类型
function CommentAll() {
$.post("/ashx/myzhuye/Detail.ashx",
{ methodName: "GetAllComment", str1: "atr1", "str2": "str2" },
function (da) {
$.each(da, function (i, v) {
alert(v);
});
}, "json");
}
扩展:https://www.cnblogs.com/gxbk629/p/5795568.html
ajax调用ashx页面内的方法
最新推荐文章于 2025-02-14 12:31:30 发布