liyifan 2012-09-27
1. html页面全部代码
<html>
<head>
<title></title>
<scriptsrc="../../Scripts/jquery-1.4.1.min.js"type="text/javascript"></script>
<scriptsrc="../../Scripts/JqueryJson.js"type="text/javascript"></script>
<scripttype="text/javascript">
$(function(){
$("#json").click(function () {//数组里的字段的命名和类型要和一般处理程序里定义的类里的变量要一样
//否则会出问题
varpostdata=newArray();
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>
<inputtype="button"value="json"id="json"/>
</body>
</html>2.json.ashx页面全部代码
<%@ WebHandler Language="C#" class="json"%>
using System;
usingSystem.Web;
usingSystem.Web.Script.Serialization;
using System.Collections.Generic;public class json : IHttpHandler {
publicvoidProcessRequest(HttpContextcontext){
context.Response.ContentType="text/plain";
//接受出过来的值string sun = context.Request["array"].ToString();
//实例化JavaScriptSerializer对象
JavaScriptSerializerjss=newJavaScriptSerializer(); List<array> a = newList<array>();
//把json转换其他list<array>类型
a=jss.Deserialize(sun,typeof(List<array>))asList<array>;
stringmeg=null;
foreach(varitemina)
{
meg+=item.number;
}
context.Response.Write(meg);
}public class array
{
publicintid{get;set;}
publicstringnumber{get;set;}
}
publicboolIsReusable{
get{
returnfalse;
}
}}
我的代码:
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; } } } }