您好,欢迎访问一九零五行业门户网

详解jQuery getJSON是如何处理json中的数据代码

ashx处理程序:如果需要返回json格式的对象,需要把mime类型设置为:application/json。
html代码:
代码如下:
<script type="text/javascript" src="/js/jquery-1.4.js"></script> <script type="text/javascript"> function jsontest1() { $.ajax({ url:"handler.ashx", data:{"type":"ajax"}, datatype:"json", type:"get", success:function(data) { document.getelementbyid('p1').innerhtml=data;//因为mime类型是文本 所以返回回来的是json格式的字符串 } }); } function jsontest2() { $.getjson( 'handler.ashx', {'type': 'json','name':'qixuejia' }, //类型格式 function(data) { for(var i=0;i<data.length;i++) { alert(data[i]["userid"]) } } ); } </script> <form id="form1" runat="server"> <p id="p1"> </p> <input type="button" value="jquery.ajax()" onclick="jsontest1()"/> <input type="button" value="jquery.getjson()" onclick="jsontest2()"/> </form>
ashx处理程序:如果需要返回json格式的对象,需要把mime类型设置为:"application/json"。
查看jquery源文件,可以看出getjson这样实现的:
getjson: function( url, data, callback ) {
return jquery.get(url, data, callback, "json");
},
代码如下:
public void processrequest(httpcontext context) { if (context.request.params["type"].equals("ajax")) { context.response.contenttype = "text/plain"; } else { context.response.contenttype = "application/json"; } getinfo(context); } public bool isreusable { get { return false; } } public void getinfo(httpcontext context) { system.collections.generic.list<userinfo> listuser = userinfomanage.getuserinfobysql("select top 5 * from userinfo"); isodatetimeconverter timeconverter = new isodatetimeconverter(); timeconverter.datetimeformat = "yyyy'-'mm'-'dd' 'hh':'mm':'ss"; string resjsonstr = jsonconvert.serialize object (listuser, timeconverter); context.response.write(resjsonstr); }
以上就是详解jquery getjson是如何处理json中的数据代码的详细内容。
其它类似信息

推荐信息