1.WebService代码
1 [WebMethod] 2 [ScriptMethod(UseHttpGet = false)] 3 public string GetObject() 4 { 5 User user = new User() 6 { 7 UserID = 1, 8 UserName = "1111" 9 }; 10 //直接用json.net框架序列化 11 return JsonConvert.SerializeObject(user); 12 13 } 14
2.jQuery代码
1 $(document).ready(function () { 2 var btn = document.getElementById("btnQ"); 3 btn.onclick = function () { 4 Ajax(); 5 6 } 7 }); 8 function Ajax() { 9 $.ajax({ 10 type: "POST", 11 contentType: "application/json", 12 url: "WebService.asmx/GetObject", 13 dataType: "json", 14 success: function (data) { 15 var ruleListTemp = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"; 16 //注:从WebService获取json格式为 17 //{"d":"{\"UserID\":1,\"UserName\":\"1111\"}"} 18 //所以要加一步操作 19 var json = jQuery.parseJSON(data.d); 20 $.each(json, function (n, value) { 21 ruleListTemp += ("<tr><td>" + value); 22 ruleListTemp += ("</td></tr>"); 23 }); 24 ruleListTemp += ("</table>"); 25 26 $("#ruleList").html(ruleListTemp); 27 28 29 } 30 31 }); 32 }
3.html代码
1 <input id="btnQ" type="button" value="请求数据"/> 2 <div id="ruleList"> 3 4 </div>
原文:http://www.cnblogs.com/aasd147789/p/4986068.html