js 调用webservice 的某个方法
Service.asmx
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 GetUserName(string UserName) {
return UserName;
}
}
js代码
function RequestByGet(data){
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//Webservice
location.
var URL="http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach";//指定webservice方法,?后指定参数和参数value
xmlhttp.Open("GET",URL,
false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
// XMLHTTP 默认的情况下有些参数可能没有说明在HTTP头里,这是当我们需要修改或添加这些参数时就用到
xmlhttp.Send(data);
var result = xmlhttp.status;
//OK
if(result==200) {
document.write(xmlhttp.responseText);
}
xmlhttp = null;
}
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 + '<GetUserName
xmlns="http://tempuri.org/">';
//webservice里面的方法名称
data = data + '<UserName>'+value+'</UserName>'; //这里是方法参数
data = data +
'</GetUserName>';
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);
}
<input type="button" value="CallWebserviceByPost" onClick="RequestByPost('Zach')">

浙公网安备 33010602011771号