您的位置:首页 > Web前端 > JavaScript

发送Ajax请求获取JSON格式数据

2013-11-22 16:45 1066 查看
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;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐