JS同步调用WEBSERVICE

这个是同步的,你也可以用Ajax异步调用

 

代码
1 <html>
2  <head><title>Message</title></head>
3  <script>
4  function subinfo()
5 {
6  var obj = document.getElementById("sender").value;
7  var data;
8 data = '<?xml version="1.0" encoding="utf-8"?>';
9 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/">';
10 data = data + '<soap:Body>';
11 data = data + '<HelloWorld xmlns="http://www.xujie.org/">';
12 data = data + '<str>'+obj+'</str>';
13 data = data + '</HelloWorld>';
14 data = data + '</soap:Body>';
15 data = data + '</soap:Envelope>';
16 try{
17 var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
18 var URL="http://localhost:4002/WebS/Service.asmx";
19 xmlhttp.Open("POST",URL, false);
20 xmlhttp.SetRequestHeader("POST","/WebS/Service.asmx HTTP/1.1");
21 xmlhttp.SetRequestHeader("Host","localhost");
22 xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
23 xmlhttp.SetRequestHeader ("SOAPAction","http://www.xujie.org/HelloWorld");
24 xmlhttp.Send(data);
25 }catch(Ex){document.getElementById("res").innerHTML = "Web服务不可用,或是主机没有连接网络"}
26
27 var result = xmlhttp.status;
28 if(result==200) {
29 document.getElementById("res").innerHTML = xmlhttp.responseText;}
30 }
31 </script>
32 <body>
33 Content:
34 <input type="textarea" value="" id="sender" />
35 <input type="button" onclick="subinfo();" value="Said" id="login"/>
36 <div id="res"></div>
37 </body>
38 </html>
39
40
41
42

 

 

 

经过测试 没有问题!

 

webservice 就是 新建的一个helloWorld

 

posted @ 2010-12-02 16:45  莫问哥哥  阅读(506)  评论(0)    收藏  举报