Javascript+xmlhttp調用Webservice(一)。

來源:互聯網
上載者:User
原文:http://www.cnblogs.com/netboy/archive/2006/02/18/333260.html
1.  建立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;
    }
    
}

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方法時發送的資料多的原因嗎?

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.