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\t11\t11\r\n22\t22\t22\r\n33\t33\t33\r\n"); // \t制表转义符(空格效果) \r\n换行转义符
failedMessage.Append(@"11\t11\t11\r\n22\t22\t22\r\n33\t33\t33\r\n");
string result = "结果信息";
// \\r\\n转义后为\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;
}
}
}
}