Javascript+xmlhttp调用Webservice。

1.  创建webservice,为了免于落俗我稍稍修改了创建webserice的默认webmethod。^_^
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"?>';
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的测试页面中找到,自己拼凑加上对应的参数就可以。

我发现用post方法的时候响应很慢,是因为用Post方法时发送的数据多的原因吗?





posted @ 2006-02-18 21:34 zhanqiangz(闲云野鹤) 阅读(8659) 评论(5)  编辑 收藏

用vs2005调用第一种方法失败
调用第二种方法 返回xmlhttp.responseText的内容巨多,还要重新定位

  回复  引用  查看    
#2楼2007-04-29 17:44 | Jeffrey Zhao      
JavaScript使用SOAP来调用WEb Service实在是件不讨好的事情啊……
  回复  引用    
#3楼2007-06-09 19:40 | colin[未注册用户]
我用了post方法,但是在参数传递中我用了XML
意思是说我把data = data + '<Name>'+value+'</Name>';
中value换成了一个XML内容
var xmlPara = "<strmessage><Touser>colin</Touser><message>hello</message><Fromuser>dolphin</Fromuser></strmessage>"
却不成功,在调试页面,无法调用WebService的函数?怎么解决?

@colin
我感觉你的xml文本做为value传的时候需要HtmlEncode,但是我自己没有做尝试,你可以自己亲自尝试一下。

  回复  引用  查看    
#5楼2009-04-20 22:45 | Armslave      
我在用楼主的方法做的时候如果节点之间包含一些特殊的字符的时候,就会报错, 例如& 或者+,转义也不管用,不知道楼主可否遇到过



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 333260




相关文章:

相关链接: