Aspx前端页面:
<script type="text/javascript"> $(function () {
$.getJSON("Ajax/TestAjax.ashx?_=" + Math.random(), function (json) { //发送请求 $('#result').html(json.result); $('#message').html(json.message); }); $('#<%=btnAdd.ClientID %>').click=function () { } }); </script>
ajax一般处理程序页面:
namespace WebFormTest.Ajax { /// <summary> /// $codebehindclassname$ 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class TestAjax : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //string json ="{"Person":[{"Age":18,"Name":"张三","Sex":"男"}]}";
StringBuilder successMessage = new StringBuilder();
StringBuilder failedMessage = new StringBuilder();
successMessage.Append(@"11 11 11 22 22 22 33 33 33 "); // 制表转义符(空格效果) 换行转义符
failedMessage.Append(@"11 11 11 22 22 22 33 33 33 ");
string result = "结果信息";
// \r\n转义后为 在前端浏览器解析后为换行 string message = "发放失败:\r\n" + failedMessage.ToString() + "\r\n\r\n发放成功:\r\n" + successMessage.ToString();
string json = "{"result":"" + result + "","message":"" + message + ""}"; //注意转义 context.Response.Write(json); } public bool IsReusable { get { return false; } } } }