这里记录了javascript调用c#.net的webservice服务的调试代码,希望对大家有帮助
启动microsoft visual studio C# 文件--新建--网站-- ASP.NET Web服务 会自动产生如下代码:
using System; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq;
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class Service : System.Web.Services.WebService { public Service () {
//如果使用设计的组件,请取消注释以下行 //InitializeComponent(); }
[WebMethod] public string HelloWorld() { return "Hello World"; } }
其中的 [WebMethod] public string HelloWorld() { return "Hello World"; } 这段就是由你来写的服务程序,可以改成如下: [WebMethod] public string HelloWorld(string name) { return "Hello " + name;} [WebMethod] public string TestJS(int a,int b) { return a+"+"+b+"="+(a+b); }
然后运行,可以看到运行结果有 HelloWorld 和 TestJS 两个链接,点击可以看到测试页。
下面做个html网页,引用刚运行的网址,用javascript来调用以上的webservice服务: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>Call webservice with javascript and xmlhttp.</title> </head> <body> <script language="javascript"> //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' "; data = data + " xmlns:xsd='http://www.w3.org/2001/XMLSchema' "; data = data + " xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' "; data = data + " xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' "; data = data + ">"; data = data + '<soap:Body>'; data = data + '<HelloWorld xmlns="http://tempuri.org/">'; data = data + '<name>'+value+'</name>'; data = data + '</HelloWorld>'; data = data + '</soap:Body>'; data = data + '</soap:Envelope>'; var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); var URL="http://localhost:42902/WebSite2/Service.asmx"; xmlhttp.Open("POST",URL, false); xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8"); xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/HelloWorld"); xmlhttp.Send(data); cont.value= xmlhttp.ResponseXML.xml; } function getwstest(value){ var data=""; data="<xml version='1.0' encoding='utf-8'>"; data = data + "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "; data = data + " xmlns:xsd='http://www.w3.org/2001/XMLSchema' "; data = data + " xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' "; data = data + " xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' "; data = data + ">"; data = data + "<soap:Body>"; data = data + "<TestJS xmlns='http://tempuri.org/'>"; //注意,TestJS是WebService里的方法名 data = data + "<a>";//TestJS方法里的参数 data = data + value; data = data + "</a>"; data = data + "<b>";//TestJS方法里的参数 data = data + "33"; data = data + "</b>"; data = data + '</TestJS>'; data = data + '</soap:Body>'; data = data + '</soap:Envelope>'; var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); var URL="http://localhost:42902/WebSite2/Service.asmx"; xmlhttp.Open("POST",URL, false); xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8"); xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/TestJS");// tempuri.org是WebService添加的命名空间 //就是c#里的[WebService(namespace = "http://tempuri.org/")]这一行 xmlhttp.Send(data); var w= xmlhttp.responseXML.text;//取得返回值 delete xmlhttp; xmlhttp = null; cont.value=w; return w; } </Script> <input type=text id=nam1> <input type="button" value="hello name xml" onClick="RequestByPost(nam1.value)"> <input type="button" value="数字加法 text" onClick="getwstest(nam1.value)"> <br> <textarea id=cont rows=25 cols=80></textarea> </body> </html> 上例分别用了两种方法,展示了两种结果(xml格式和text格式) var URL="……" 这句根据实际网址来修改 命名空间http://tempuri.org/与c#代码里的写法要一致 其它代码应该看得懂吧,可以任意发挥了。 |