Discuz教程网

Jquery ajax传递复杂参数给WebService的实现代码

[复制链接]
authicon dly 发表于 2011-9-12 15:19:29 | 显示全部楼层 |阅读模式
Entity:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.Serialization;
  6. namespace Entity
  7. {
  8. [DataContract]
  9. public class User
  10. {
  11. [DataMember]
  12. public string Name
  13. {
  14. get;
  15. set;
  16. }
  17. [DataMember]
  18. public int Age
  19. {
  20. get;
  21. set;
  22. }
  23. }
  24. }
复制代码

WebService:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. using Entity;
  7. namespace JQuery.Handler
  8. {
  9. [WebService(Namespace = "http://tempuri.org/")]
  10. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  11. [System.ComponentModel.ToolboxItem(false)]
  12. [System.Web.Script.Services.ScriptService]
  13. public class UserService1 : System.Web.Services.WebService
  14. {
  15. [WebMethod]
  16. public string ComplexType(User hero,List<User> users)
  17. {
  18. return hero.Name + " has " + users.Count + " people!";
  19. }
  20. }
  21. }
复制代码

Html:
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head>
  3. <title>Ajax</title>
  4. <script src="../Scripts/jquery-1.6.min.js" type="text/javascript"></script>
  5. <script type="text/javascript">
  6. $(function () {
  7. $("#btnWeb").click(function () {
  8. $.ajax(
  9. {
  10. type: "post",
  11. url: "../Handler/UserService.asmx/ComplexType",
  12. dataType:"json",
  13. contentType:"application/json",
  14. data: '{"hero": {"Name":"zhoulq","Age":27},"users":[{"Name":"zhangs","Age":22},{"Name":"wangw","Age":26},{"Name":"liuj","Age":25},
  15. {"Name":"luos","Age":24}]}',
  16. success: function (data) { $("#web").text(data.d); }
  17. });
  18. });
  19. });
  20. </script>
  21. </head>
  22. <body>
  23. <input id="btnWeb" type="button" value="请求WebService" /><label id="web"></label>
  24. </body>
  25. </html>
复制代码



上一篇:jquery学习笔记 用jquery实现无刷新登录
下一篇:基于jQuery实现的水平和垂直居中的div窗口
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 20:28

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表