Javascript调用Webservice的多种方法

代码如下:

  1. using System;  
  2. using System.Web;  
  3. using System.Web.Services;  
  4. using System.Web.Services.Protocols;  
  5. [webservice(namespace = “http://tempuri.org/”)]  
  6. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  7. public class Service : System.Web.Services.WebService  
  8. {  
  9. public Service ()  
  10. {  
  11. //uncomment the following line if using designed components  
  12. //InitializeComponent();  
  13. }  
  14. [webmethod]  
  15. public string SayHelloTo(string Name)  
  16. {  
  17. return “Hello “+Name;  
  18. }  
  19. }  

2. js调用webservice+xmlhttp的实现部分。

代码如下:

  1. <html>  
  2. <title>Call webservice with javascript and xmlhttp.</title>  
  3. <body>  
  4. <script language=“javascript”><!–  
  5.  
  6.  
  7. //test function with get method.  
  8. function RequestByGet(data){  
  9. var xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);  
  10. //Webservice location.  
  11. var URL=“http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach”;  
  12. xmlhttp.Open(“GET”,URL, false);  
  13. xmlhttp.SetRequestHeader (“Content-Type”,”text/xml; charset=utf-8″);  
  14. xmlhttp.SetRequestHeader (“SOAPAction”,”http://tempuri.org/SayHelloTo”);  
  15. xmlhttp.Send(data);  
  16. var result = xmlhttp.status;  
  17. //OK  
  18. if(result==200) {  
  19. document.write(xmlhttp.responseText);  
  20. }  
  21. xmlhttp = null;  
  22. }  
  23.  
  24. //test function with post method  
  25. function RequestByPost(value)  
  26. {  
  27. var data;  
  28. data = ‘<?xml version=”1.0″ encoding=”utf-8″?>’;  
  29. datadatadata = data + ‘<soap:Envelope xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=“http://www.w3.org/2001/XMLSchema” xmlns:soap=“http://schemas.xmlsoap.org/soap/envelope/”>‘;  
  30. datadatadata = data + ‘<soap:Body>‘;  
  31. datadatadata = data + ‘<SayHelloTo xmlns=“http://tempuri.org/”>‘;  
  32. datadatadata = data + ‘<Name>‘+value+’</Name>‘;  
  33. datadatadata = data + ‘</SayHelloTo>‘;  
  34. datadatadata = data + ‘</soap:Body>‘;  
  35. datadatadata = data + ‘</soap:Envelope>‘;  
  36.  
  37. var xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);  
  38. var URL=“http://localhost:1323/WebSite6/Service.asmx”;  
  39. xmlhttp.Open(“POST”,URL, false);  
  40. xmlhttp.SetRequestHeader (“Content-Type”,”text/xml; charset=gb2312“);  
  41. xmlhttp.SetRequestHeader (“SOAPAction”,”http://tempuri.org/SayHelloTo”);  
  42. xmlhttp.Send(data);  
  43. document.write( xmlhttp.responseText);  
  44. }  
  45.  
  46.  
  47. // —></script>  
  48.  
  49. <input type=“button” value=“CallWebserviceByGet” onClick=“RequestByGet(null)”>  
  50. <input type=“button” value=“CallWebserviceByPost” onClick=“RequestByPost(‘Zach’)”>  
  51. </body>  
  52. </html> 

对于使用post方法需要发送的那堆东东可以在webservice的测试页面中找到,自己拼凑加上对应的参数就可以。

通过style.behavior来实现的方法(比较简单)

function getfemale()

{

//第一个参数是webservice的url,后面是名称

female.useService(“news.asmx?WSDL”,”news”);

//设置一个回调函数,service返回结果的时候回调;第一个参数是回调函数的名称,后面的是webservice的参数

intCallID=female.news.callService(female_result,”getphoto”,”female”); //这里有两个参数…..

}

function female_result(result)//回调函数

{

if(result.error)

{

female.innerHTML=result.errorDetail.string;

}

else

{

female.innerHTML=result.value; //将webservice返回的结果写如div中

}

}

页面显示部分:  

ok,这给我们在静态页调用动态的内容提供了一种途径;

这里如果给getfemale()函数加上定时调用的话,就是一种无刷新更新页面的机制了。

缺点是webservice会有一定的延迟,即使是本地的webservice也会比静态页面慢很多,初次打开页面会感觉很不协调。

第二种方法使用了style.代码就简洁多了他使用了css.定义了div的行为.比起第一种方法,就易读多了:)

style=”behavior:url(webservice.htc)”

前提条件是:

if you are using Microsoft IE 5 or later, you can use the behavior/HTML-Component “WebService” to access a Web service. The “WebService” behavior communicates with Web services over HTTP using Simple Object Access Protocol (SOAP).

转载请注明:代码学堂>编程开发 > ASP.NET > Javascript调用Webservice的多种方法

1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!10210454@qq.com
2. 本站不保证所提供所有下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理,有奖励!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
4. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有RB奖励和额外RMB收入!

磊宇堂正在使用的服务器 维护管理由磊宇云服务器提供支持

磊宇堂 » Javascript调用Webservice的多种方法

提供最优质的资源集合

立即查看 了解详情