ajax向ashx请求json数据

js代码
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(function(){
//1.返回数据类型 dataType: 'json'
//2.传递的数据格式 为空时:data: {}, 不为空时:data: {username:"Lee"},
//3.返回的json数据格式 单条数据:{"key","value"} , 多条数据:[{"key","value"},{"key","value"}]
$.ajax({
async:true,
type:"POST",
contentType: "application/json",
dataType: 'json', //返回数据类型
url: "1.ashx",
data: {},
//data: {username:username},
success: function(msg) {
alert(msg);
for(i=0;i<msg.length;i++)
{
$("#rt").append(msg[i].name+"__"+msg[i].age+"<br/>");
}

},
error: function(){alert("error");}
});
});
</script>
html代码
<div id="rt">

</div>

 

c#
context.Response.Cache.SetNoStore();
StringBuilder returnValue=new StringBuilder();
returnValue.Append("[");
returnValue.Append("{\"name\":\"Lee\",\"age\":\"28\"},");
returnValue.Append("{\"name\":\"Lucy\",\"age\":\"25\"}");
returnValue.Append("]");
context.Response.Write(returnValue.ToString());



posted on 2012-03-12 11:19  woshilee  阅读(278)  评论(0)    收藏  举报

导航