当前位置:首页 > 网络编程 > 软件语言 > .NET > Javascript调用Webservice的汇集

Javascript调用Webservice的汇集

点击次数:88 次 发布日期:2008-11-06 08:10:54 作者:源代码网
源代码网推荐
广告载入中

源代码网整理以下整理了一下:js调用WebService的几种方法:
通过xmlhttp+webservice(原始方法)

源代码网整理以下using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

源代码网整理以下[webservice(namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {

源代码网整理以下//uncomment the following line if using designed components
//InitializeComponent();
}

源代码网整理以下[webmethod]
public string SayHelloTo(string Name) {
return "Hello "+Name;
}

源代码网整理以下}
软件开发网 www.mscto.com

源代码网整理以下2. js调用webservice+xmlhttp的实现部分。

软件开发网 www.mscto.com

源代码网整理以下<html>
<title>
Call webservice with javascript and xmlhttp.
</title>
<body>
<script language="javascript">

源代码网整理以下//test function with get method.
function RequestByGet(data){

源代码网整理以下var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//Webservice location.
var URL="http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach";
xmlhttp.Open("GET",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
xmlhttp.Send(data);
var result = xmlhttp.status;
//OK
if(result==200) {
document.write(xmlhttp.responseText);
}
xmlhttp = null;
}

源代码网整理以下//test function with post method
function RequestByPost(value)
{
var data;
data = "<?xml version="1.0" encoding="utf-8"?>";
data = 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/">";
data = data + "<soap:Body>";
data = data + "<SayHelloTo xmlns="http://tempuri.org/">";
data = data + "<Name>"+value+"</Name>";
data = data + "</SayHelloTo>";
data = data + "</soap:Body>";
data = data + "</soap:Envelope>";

源代码网整理以下var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var URL="http://localhost:1323/WebSite6/Service.asmx";
xmlhttp.Open("POST",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=gb2312");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
xmlhttp.Send(data);
document.write( xmlhttp.responseText);

源代码网整理以下}

源代码网整理以下</script>

源代码网整理以下<input type="button" value="CallWebserviceByGet" onClick="RequestByGet(null)">
<input type="button" value="CallWebserviceByPost" onClick="RequestByPost("Zach")">

源代码网整理以下</body>
</html>
对于使用post方法需要发送的那堆东东可以在webservice的测试页面中找到,自己拼凑加上对应的参数就可以。

源代码网整理以下通过style.behavior来实现的方法(比较简单)

源代码网整理以下 

源代码网整理以下<script language="javascript">
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中
}
}
</script>
页面显示部分: <div id="female" style="BEHAVIOR:url(WebService.htc)"></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).

源代码网整理以下calling WebServices using Javascript

源代码网整理以下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).

源代码网整理以下to use the "WebService" behavior, you must attach it to an element using the STYLE attribute, as follows:

源代码网整理以下  软件开发网 www.mscto.com

源代码网整理以下style="behavior:url(webservice.htc)">


源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华