利用ASP.net Ajax 调用web service 里面的方面,简单易学的例子:
(1) 配置webservice
1:引用:using System.Web.Script.Services;
2:在类前写上属性[ScriptService ]表示webservice method可以被javascript调用——具体代码为
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
[ScriptService ]
2
public class WebService1 : System.Web.Services.WebService
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
[WebMethod ]
6
[ScriptMethod]
7
public string SayHelloByName(string name)
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
return "Helllo" +" " + name;
10
}
11
}
3:配置webservice完成
(2)web.config配置:
1:添加以下配置
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
<!-- add webServer for clientServer-->
2
<configSections>
3
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
4
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
5
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
6
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
7
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere"/>
8
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
9
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
10
</sectionGroup>
11
</sectionGroup>
12
</sectionGroup>
13
</configSections>
14
//下面的 <httpHandlers>, <httpModules>添加在<system.web>里面
15
<!-- add webServer for clientServer-->
16
<httpHandlers>
17
<remove verb="*" path="*.asmx"/>
18
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
19
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
20
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
21
</httpHandlers>
22
<!-- add webServer for clientServer-->
23
<httpModules>
24
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
25
</httpModules>
26
<!-- add webServer for clientServer-->
27
<system.webServer>
28
<validation validateIntegratedModeConfiguration="false"/>
29
<modules>
30
<add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
31
</modules>
32
<handlers>
33
<remove name="WebServiceHandlerFactory-Integrated"/>
34
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
35
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
36
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
37
</handlers>
38
</system.webServer>
(3) 前台*.aspx
(1)投放 <asp:ScriptManager>控件,调用WebMeth
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="WebService1.asmx" />//调用WebMethod
</Services>
</asp:ScriptManager>
(2) OK,最后就是效果展示
<script language="javascript" type="text/javascript">
// <!CDATA[
function Button1_onclick() {
var message = OneDemo.WebService1.SayHelloByName(document.getElementById("Text1").value,OnComplete,OnErron);
return true;
}
function OnComplete(arg) {
alert(arg);
}
function OnErron(arg) {
alert("It 's Ocurre error");
}
// ]]>
</script>
<input id="Text1" type="text" />
<input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
![](https://images.cnblogs.com/cnblogs_com/skyyang/Snap1.jpeg)
3:Demo is complete,以后陆续把asp.net ajax 完整的Demo 写完