JSONPPage

 

public class JSONPPage : System.Web.UI.Page
{
protected override void OnPreRender(EventArgs e)
{
var sb
= new StringBuilder();

var writer
= new HtmlTextWriter(new StringWriter(sb));

Page.RenderControl(writer);

var data
= new JavaScriptSerializer().Serialize(sb.ToString());

// ---- 解决不同网站不同编码造成中文乱码的问题 ,如不需要可注释掉提交效率 --------
var tempSB = new StringBuilder();
var pf
= "\\u";
foreach (char s in data)
{
if (s > 128)
tempSB.Append(pf
+ ((int)s).ToString("x"));
else
tempSB.Append(s);
}
data
= tempSB.ToString();
// ------------------------------------------------------------------

writer.Close();

Response.Write(String.Format(
"{0}({1});", Request["callback"] ?? Request["jsonp"], data));

Response.End();
}
}

 

 

posted @ 2010-05-14 17:24  王其荣的博客  阅读(296)  评论(0编辑  收藏  举报