js调用webservice中的方法
webservice代码:
1 using System; 2 using System.Web; 3 using System.Collections; 4 using System.Web.Services; 5 using System.Web.Services.Protocols; 6 using System.Web.Script.Services; 7 using StoreHouse.Common; 8 using StoreHouse.IDAL; 9 using StoreHouse.Model; 10 using System.Data.SqlClient; 11 using System.Data; 12 using System.Xml; 13 using System.Collections.Generic; 14 using System.Runtime.InteropServices; 15 using System.Net.NetworkInformation; 16 17 /// <summary> 18 /// PageService 的摘要说明 19 /// </summary> 20 [WebService(Namespace = "http://tempuri.org/")] 21 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 22 [ScriptService] 23 public class PageService : System.Web.Services.WebService 24 { 25 ArrayList al = new ArrayList(); 26 public PageService() 27 { 28 29 //如果使用设计的组件,请取消注释以下行 30 //InitializeComponent(); 31 } 32 /// <summary> 33 /// 添加收藏 34 /// </summary> 35 /// <param name="fid">产品ID</param> 36 /// <param name="sid">用户ID</param> 37 /// <param name="flag">标识(产品收藏、企业收藏、电子样本收藏、图库收藏)</param> 38 /// <returns>返回收藏状态</returns> 39 [WebMethod] 40 public int AddCollect(int fid,int sid,int flag) 41 { 42 IProductDataProvider ipdp = ProviderManager.Create<IProductDataProvider>(); 43 IEnterpriseDataProvider iedp = ProviderManager.Create<IEnterpriseDataProvider>(); 44 if (flag == 1) 45 { 46 ProductCollect pc = new ProductCollect(); 47 pc.ProductID = fid; 48 pc.UserID = sid; 49 pc.AddDatetime = DateTime.Now; 50 pc.UpdateDatetime = DateTime.Now; 51 return ipdp.AddProductCollect(pc); 52 } 53 else if (flag == 2) 54 { 55 EnterpriseCollect ec = new EnterpriseCollect(); 56 ec.EnterpriseID = fid; 57 ec.UserID = sid; 58 ec.AddDatetime = DateTime.Now; 59 ec.UpdateDatetime = DateTime.Now; 60 return iedp.AddEnterpriseCollect(ec); 61 } 62 else if (flag == 3) 63 { 64 EElecSampleCollect epesc = new EElecSampleCollect(); 65 epesc.EElecSampleID = fid; 66 epesc.UserID = sid; 67 epesc.AddDatetime = DateTime.Now; 68 epesc.UpdateDatetime = DateTime.Now; 69 return ipdp.AddEElecSampleCollect(epesc); 70 } 71 else if (flag == 4) 72 { 73 EPAtlasCollect epac = new EPAtlasCollect (); 74 epac.EPAtlasID= fid; 75 epac.UserID = sid; 76 epac.AddDatetime = DateTime.Now; 77 epac.UpdateDatetime = DateTime.Now; 78 return ipdp.AddEPAtlasCollect(epac); 79 } 80 return 000000; 81 }
js代码:
1 function AddAnswerResult(result) { 2 document.getElementById('dlAnswer').innerHTML += "<table Width='100%'>" + result + "</table>"; 3 4 document.getElementById('taAnswerContent').value = ""; 5 alert('提交成功'); 6 }
页面代码:
1 <body> 2 <form id="form1" runat="server"> 3 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> 4 <Services> 5 <asp:ServiceReference Path="../Service/PageService.asmx" /> 6 </Services> 7 </asp:ScriptManager> 8 9 <asp:Label ID="dlAnswer" runat="server" Style="word-wrap: break-word; word-break: normal; 10 11 </body>
最后在配置文件中需要添加:
1 <system.web> 2 <httpHandlers> 3 <remove verb="*" path="*.asmx"/> 4 <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 5 6 </httpHandlers> 7 </system.web>
这样,在客户端脚本中就可以直接调用 Web 服务的方法了。
蜗牛之狂飙,继续努力,不要停止你的脚步!