第一种:ajax调用本页面后台的方法GetImgHeadInfos
前台ajax方法调用:
$.ajax({
type: "post",
data: "{\"ProjectRGuid\": \"" + op.ProjectRGuid + "\", \"ParentGuid\": \"" + op.ParentGuid + "\", \"StageTypeRGuid\": \"" + op.StageTypeRGuid + "\" }",
contentType: "application/json;utf-8",
url: "FileList.aspx/GetImgHeadInfos",
success: function (data) {
var attachInfos = $.parseJSON(data.d);
op.head[op.ModuleType] = attachInfos;
if (attachInfos.length == 0) {
//取消蒙版
$(".modalBackground").css("display", "none");
$("#divPicTitle").empty();
$(".ad-image").empty();
//$(".ad-info").html("<span class='font2'>共0页</span>");
$(".imgtools").css("display", "none");
$(".font1").css("display", "none");
$(".font2").html("共 0 页");
op.galleries[0].next_link.hide();
$(".ad-image-wrapper").html('<div class="nofile"><div class="picbg alert"/><div class="nofileinfo">暂无上传扫描件!</div></div>');
return;
}
else {
$(".imgtools").css("display", "");
$(".font1").css("display", "");
$("#gallery").css("display", "");
}
MakeChengXin(attachInfos, op);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
后台方法GetImgHeadInfos声明:
后台上面应引用
using System.Web.Services;
[WebMethod(EnableSession = true)]
public static string GetImgHeadInfos(string StageTypeRGuid, string ProjectRGuid, string ParentGuid)
{
try
{
string sql = @"select ProjectFiles.*,Frame_AttachInfo.AttachFileName from ProjectFiles,Frame_AttachInfo
where ProjectFiles.AttachRowGuid=Frame_AttachInfo.RowGuid and StageTypeRGuid='" + StageTypeRGuid + "' and ProjectRGuid='" + ProjectRGuid + "' and ProjectFiles.ParentGuid='" + ParentGuid + "'";
DataView DvPaging = DB.ExecuteDataView(sql);
List<AttachInfo> ls = new List<AttachInfo>();
foreach (DataRowView drv in DvPaging)
{
AttachInfo ai = new AttachInfo();
ai.UploadUserDisplayName = drv["OperateUserName"].ToString().Replace("'", "").Replace("\"", "");
ai.UploadDateTime = drv["OperateDate"].ToString().Replace("'", "").Replace("\"", "");
ai.AttachFileName = drv["AttachFileName"].ToString().Replace("'", "").Replace("\"", "");
ai.AttachGuid = drv["AttachRowGuid"].ToString().Replace("'", "").Replace("\"", "");
ai.AttachConName = "RongGuangMis_ConnectionString";
ai.informationSource = "123";
ls.Add(ai);
}
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Serialize(ls);
}
catch (Exception err)
{
throw new Exception(err.ToString());
}
}
第二种用法,ajax调用一般处理程序请求json数据
bin里引用Newtonsoft.Json.dll
$.ajax({
type: "post",
cache: false,
dataType: "json",
async: false,
url: "../../ajax/ProjectCounts_Ajax.ashx?StartTime=" + STime + "&EndTime=" + ETime + "&OType=" + opeType,
success: function (data) {
for (var i = 0; i < data.length; i++)
{
//中间有省略代码
if (y == data[i].Year) //从json中取值
{
}
}
}
});
一般处理程序
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = “text/plain”;
DateTime StartTime = Convert.ToDateTime(context.Request[“StartTime”]);
DateTime EndTime = Convert.ToDateTime(context.Request[“EndTime”]);
typeValue = Convert.ToString(context.Request[“OType”]);
typeName = GetDictionaryName(typeValue);
json = JsonConvert.SerializeObject(GetDataByTypeYear(StartTime, EndTime, typeName));
context.Response.Write(json);
}
public List GetDataByTypeYear(DateTime Year1, DateTime Year2, string type)
{
List AllCount = new List();
CountMode ItemCount = new CountMode();//模型
//循环遍历数据赋值
AllCount.Add(ItemCount);
return AllCount;
}
public class CountMode
{
public string Year
{
get;
set;
}
public string Name
{
get;
set;
}
}