Ajax的前后台封装

2021-04-19 13:25

阅读:346

标签:script   tpc   add   obj   后台   error   roo   nbsp   converter   

1.get传值(同步)

function ajaxHandler(className, functionName, data, successFunc, errorFunc) {
var path = getRootPath();
var Para = "?className=" + className + "&functionName=" + functionName + "&ran=" + new Date().getUTCMilliseconds();
$.ajax({
type: "GET",
url: path + "/Handler/HandlerAll.ashx" + Para,
data: data,
success: function (datd) {
successFunc(datd);
},
error: function () {
if (arguments.length == 5) {
errorFunc();
}
}
});
}

2.post传值(同步)

function ajaxPostHandler(className, functionName, data, successFunc, errorFunc) {
var path = getRootPath();
var Para = "?className=" + className + "&functionName=" + functionName + "&ran=" + new Date().getUTCMilliseconds();
$.ajax({
type: "POST",
url: path + "/Handler/HandlerAll.ashx" + Para,
data: data,
success: function (datd) {
successFunc(datd);
},
error: function () {
if (arguments.length == 5) {
errorFunc();
}
}
});
}

3.post传值(异步)

function ajaxPostHandlersync(className, functionName, data, successFunc, errorFunc) {
var path = getRootPath();
var Para = "?className=" + className + "&functionName=" + functionName + "&ran=" + new Date().getUTCMilliseconds();
$.ajax({
type: "POST",
async: false,
url: path + "/Handler/HandlerAll.ashx" + Para,
data: data,
success: function (datd) {
successFunc(datd);
},
error: function () {
if (arguments.length == 5) {
errorFunc();
}
}
});
}

4.get传值(异步)
function ajaxHandlerasync(className, functionName, data, successFunc, errorFunc) {
var path = getRootPath();
var Para = "?className=" + className + "&functionName=" + functionName + "&ran=" + new Date().getUTCMilliseconds();
$.ajax({
type: "GET",
async: false,
url: path + "/Handler/HandlerAll.ashx" + Para,
data: data,
success: function (datd) {
successFunc(datd);
},
error: function () {
if (arguments.length == 5) {
errorFunc();
}
}
});
}

 

后台:

public override void Process_Request(HttpContext context)
{
string functionName = context.Request.QueryString["functionName"];
string ClassName = context.Request.QueryString["className"];
Type type = Type.GetType(ClassName);
if (functionName.Trim() != string.Empty)
{
List objList = new List();
objList.Add(context);
string postdata = context.Request.Form["$postdata$"];
if (!String.IsNullOrEmpty(postdata))
{
string encode = context.Request.QueryString.Get("encode");
if (encode == "b")
{
postdata = Public.ToStringFromBase64(postdata);
}
dynamic DynamicObject = ConvertJson(postdata);
objList.Add(DynamicObject);
}
type.GetMethod(functionName).Invoke(null, objList.ToArray());
}
}

public dynamic ConvertJson(string json)
{
//JavaScriptSerializer jss = new JavaScriptSerializer();
//jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() });
//dynamic dy = jss.Deserialize(json, typeof(object)) as dynamic;


var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
dynamic dy = serializer.Deserialize(json, typeof(object));
return dy;
}

 

Ajax的前后台封装

标签:script   tpc   add   obj   后台   error   roo   nbsp   converter   

原文地址:https://www.cnblogs.com/wangSoft/p/12269894.html


评论


亲,登录后才可以留言!