JS调用Webservice
操作步骤:
1.新建一个WebApplication项目,取默认设置。
2.添加一个WebService,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
using System.ComponentModel; using System.Web.Services; namespace WebApplication1 { /// <summary> /// WebService1 的摘要说明 /// </summary> [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem( false )] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorldFun1() { return "Hello World" ; } [WebMethod] public string HelloWorldFun2( string str) { return "Hello World," +str; } } } |
3.准备好WebService后,编辑Default.aspx文件。代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>JS调用WebService</title>
<script type="text/javascript" language="javascript">
function func1()
{
WebApplication1.WebService1.HelloWorldFun1(onSuccess,onFail,'Span1');
}
function func2()
{
var txt=document.getElementById('Text1').value;
WebApplication1.WebService1.HelloWorldFun2(txt,onSuccess,onFail,'Span2');
}
function onSuccess(value,context)
{
document.getElementById(context).innerHTML=value;
}
function onFail(value)
{
alert(value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
<Services>
<asp:ServiceReference Path="~/WebService1.asmx" />
</Services>
</asp:ScriptManager>
<input id="Button1" type="button" value="button" onclick="func1()" /> <span id="Span1"></span>
<hr />
<input id="Text1" type="text" /><input id="Button2" type="button" value="button" onclick="func2()" /> <span id="Span2"></span>
</form>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>JS调用WebService</title>
<script type="text/javascript" language="javascript">
function func1()
{
WebApplication1.WebService1.HelloWorldFun1(onSuccess,onFail,'Span1');
}
function func2()
{
var txt=document.getElementById('Text1').value;
WebApplication1.WebService1.HelloWorldFun2(txt,onSuccess,onFail,'Span2');
}
function onSuccess(value,context)
{
document.getElementById(context).innerHTML=value;
}
function onFail(value)
{
alert(value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
<Services>
<asp:ServiceReference Path="~/WebService1.asmx" />
</Services>
</asp:ScriptManager>
<input id="Button1" type="button" value="button" onclick="func1()" /> <span id="Span1"></span>
<hr />
<input id="Text1" type="text" /><input id="Button2" type="button" value="button" onclick="func2()" /> <span id="Span2"></span>
</form>
</body>
</html>
操作说明:
1.页面中需要添加ScriptManager组件,然后在里面添加WebService引用声明。[这里的Pah可以换成网络上的WebService路径。]
2.将ScriptManager的EnablePageMethods属性设置为True。[这是必须的,否则JS不知道该WebService。]
3.调用的格式:namespace.class.method([param],[onsuccessJSHandle],[onfailHSHandle],context);
其中:A。context为上下文关联参数,这里设置后,在调用成功的处理函数处可以调用。
B。onsuccessJSHandle为调用成功后的处理函数。
C。onfailHSHandle为调用失败后的处理函数。
4.WebService编写时需注意:
A。WebService类前必须加 [System.Web.Script.Services.ScriptService]
B。WebService方法前必须加 [WebMethod]
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器