1
2
|
<span style= "color: rgb(255, 0, 0);" > //注意:该方法仅支持同网站目录调用(即相对路径,不支持网络路径),是为一种js调用C#函数的方法之一
</span> |
前台代码:
<asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="/WebService1.asmx" InlineScript="true" /> </Services> </asp:ScriptManager> <script type="text/javascript" language="javascript"> function func1() { WebApplication1.WebService1.HelloWorld(onSuccess,onFail,‘Span1‘); } function onSuccess(value,context) { document.getElementById(context).innerHTML=value; } function onFail(value) { alert(value); } </script>
后台代码:
using System.ComponentModel; using System.Web.Services; namespace WebApplication1 { [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } } }