Javascript调用Webservice的多种方法

所属分类: 网络编程 / ASP.NET 阅读数: 1278
收藏 0 赞 0 分享
复制代码 代码如下:

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;
}
}

还是俗了点。:)
2. js调用webservice+xmlhttp的实现部分。
复制代码 代码如下:

<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"?>';
datadata = 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/">';
datadata = data + '<soap:Body>';
datadata = data + '<SayHelloTo xmlns="http://tempuri.org/">';
datadata = data + '<Name>'+value+'</Name>';
datadata = data + '</SayHelloTo>';
datadata = data + '</soap:Body>';
datadata = 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来实现的方法(比较简单)
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中
}
}
页面显示部分: <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).
附注:另一个总结帖子在:http://goody9807.cnblogs.com/archive/2005/08/17/216725.html
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:
style="behavior:url(webservice.htc)">
附上ibm上面有关ajax调用webservice的文章: 
 使用 Ajax 调用 SOAP Web 服务,第 1 部分: 构建 Web 服务客户机
更多精彩内容其他人还在看

Asp.Net二级域名共享Forms身份验证、下载站/图片站的授权访问控制

我们平时一般在做图片或者文件下载权限控制的时候基本都是控制到下载页面的,当你的下载地址暴露后,浏览者就直接可以通过文件地址进行下载了,这时候也就出现了我们常说的盗链
收藏 0 赞 0 分享

在ASP.NET中下载文件的实现代码

通过ASP.NET来下载文件,这个问题可大可小,我们先从小的开始。当我们要让用户下载一个文件
收藏 0 赞 0 分享

asp.net下日期和时间处理的类库

发一个专门处理时间和日期的类库,记录以备查询
收藏 0 赞 0 分享

LINQ重写博客垃圾图片回收算法

本人博客后台管理模块有个功能,可以扫描图片上传文件夹下所有未被引用的博客
收藏 0 赞 0 分享

C#多线程Singleton(单件)模式模板

下面是一个C#多线程单件模式的代码模板。把T换成你自己的类型就可以使用了。其精妙之处就在于用lock语句锁定资源来避免多线程同时走入if语句去创建多个对象
收藏 0 赞 0 分享

URL重写及干掉ASP.NET试图状态的实现方法

URL重写已经很普遍了,但基本上大部分的URL重写都不支持页面的相对路径,所有如果想在已经开发好的项目中添加还是有压力的,第二就是例如微软的那个URL重写是根据正则表达式来处理的,那样是很好,但也有不足之处,就是不方便定位到某个页面只能有哪些参数
收藏 0 赞 0 分享

正则方式的自动小偷抓网程序

公司里面有许多数据没人去录入,做一个抓取网页的程序,以前做CMS系统的时候涉及过,不过这次的处理HTML上和以前做了些区别
收藏 0 赞 0 分享

asp.net生成缩略图实现代码

此文件imgSmall.ashx专门用来生成图片的缩略图,可以减少服务器压力,降低网络流量,初学者必备
收藏 0 赞 0 分享

asp.net richTextBox中高亮显示选中字符串或文本

最近开发程序需要对一段文本中的某个字符串进行高亮显示,网上找了下资料
收藏 0 赞 0 分享

ASP.net的验证控件浅析

前些天在做注册页面的验证的时候,用了下ASP.net的验证控件,有一些体会,特写下这篇博客,如果有朋友有不同ideas,欢迎大家留言
收藏 0 赞 0 分享
查看更多