1. html页面全部代码
<html>
<head>
<title></title>
<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/JqueryJson.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#json").click(function () {
//数组里的字段的命名和类型要和一般处理程序里定义的类里的变量要一样
//否则会出问题
var postdata = new Array();
postdata[1] = { id: 1, number: "yes" };
postdata[2] = { id: 2, number: "no" };
var postData = $.toJSON(postdata); //把数组转换成json字符串
//将json字符串反序列化,这个只是测试一下数组是否转换成json字符串
var content = $.parseJSON(postData);
$.each(content, function () {
alert(this.number);
});
//post提交并处理
$.post("json.ashx", { "array": postData }, function (data, status) {
if (status == "success") {
alert(data);
}
});
});
})
</script>
</head>
<body>
<input type="button" value="json" id="json"/>
</body>
</html>
2.json.ashx页面全部代码
<%@ WebHandler Language="C#" Class="json" %>
using System;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections.Generic;
public class json : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//接受出过来的值
string sun = context.Request["array"].ToString();
//实例化JavaScriptSerializer对象
JavaScriptSerializer jss = new JavaScriptSerializer();
List<array> a = new List<array>();
//把json转换其他list<array>类型
a = jss.Deserialize(sun, typeof(List<array>)) as List<array>;
string meg=null;
foreach (var item in a)
{
meg += item.number;
}
context.Response.Write(meg);
}
public class array
{
public int id { get; set; }
public string number { get; set; }
}
public bool IsReusable {
get {
return false;
}
}
}
我的代码:
function ClickPreViewHandle(obj) { var divid = $(obj).attr("surfaceid").replace("btnPV", "div") var inputvalues = GetSurfaceTextInputValue(divid); var inputvaluesJS = $.toJSON(inputvalues); jQuery.blockUI({ message: "Generation Image...", css: { padding: 25, color: '#fff', border: '3px solid #aaa', backgroundColor: '#507691'} }); $.ajax({ type: "post", url: "/PersonalizerPreViewHandler.ashx", //dataType: "json", data: { 'values': inputvaluesJS }, success: function (data) { var leftA = divid.replace("div", "divimg"); $("#" + leftA + " img").attr("src", data); jQuery.unblockUI(); }, error: function (err) { alert("error:" + err); jQuery.unblockUI(); } }); return false; }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Script.Serialization; using THY.Allure.BusinessEntities.BaseEntities; namespace THY.Allure.Web { /// <summary> /// Summary description for PersonalizerPreViewHandler /// </summary> public class PersonalizerPreViewHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string optionBOStr = context.Request.Params["values"]; JavaScriptSerializer jss = new JavaScriptSerializer(); List<ProductDetailOptionsBO> optionBOs = new List<ProductDetailOptionsBO>(); optionBOs = jss.Deserialize(optionBOStr, typeof(List<ProductDetailOptionsBO>)) as List<ProductDetailOptionsBO>; context.Response.Write("~/media/images/cordial/product-surface-images/Postcards/PC1476/010.ashx"); } public bool IsReusable { get { return false; } } } }