JavaScript调用web服务学习(一)
前台代码:首先要配置 ScriptManager控件中添加
<Scripts>
<asp:ScriptReference Path="JScript.js" /> //javascript脚本的路径
</Scripts>
<Services>
<asp:ServiceReference Path="WebService.asmx" /> //WebService服务的路径
</Services>
javascript脚本代码:
function Div() 中的WebService.Div(a,b,OnSucceeded,OnFailed);意思是调用web服务WebService的Div方法
WebService代码:首先添加using System.Web.Script.Services;
<Scripts>
<asp:ScriptReference Path="JScript.js" /> //javascript脚本的路径
</Scripts>
<Services>
<asp:ServiceReference Path="WebService.asmx" /> //WebService服务的路径
</Services>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="JScript.js" />
</Scripts>
<Services>
<asp:ServiceReference Path="WebService.asmx" />
</Services>
</asp:ScriptManager>
<input id="Text1" type="text" />
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="XX-Large"
ForeColor="Red" Text="/"></asp:Label>
<input id="Text2" type="text" /><asp:Label ID="Label2" runat="server"
Font-Bold="True" Font-Size="XX-Large" ForeColor="#FF3300" Text="="></asp:Label>
<asp:Label ID="Label3" runat="server" Font-Bold="True" Font-Size="XX-Large"
ForeColor="#FF3300"></asp:Label>
<input id="Button1" type="button" value="计算" onclick="Div();"/><br />
<span id="Results"></span>
</div>
</form>
</body>
</html>
<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="JScript.js" />
</Scripts>
<Services>
<asp:ServiceReference Path="WebService.asmx" />
</Services>
</asp:ScriptManager>
<input id="Text1" type="text" />
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="XX-Large"
ForeColor="Red" Text="/"></asp:Label>
<input id="Text2" type="text" /><asp:Label ID="Label2" runat="server"
Font-Bold="True" Font-Size="XX-Large" ForeColor="#FF3300" Text="="></asp:Label>
<asp:Label ID="Label3" runat="server" Font-Bold="True" Font-Size="XX-Large"
ForeColor="#FF3300"></asp:Label>
<input id="Button1" type="button" value="计算" onclick="Div();"/><br />
<span id="Results"></span>
</div>
</form>
</body>
</html>
javascript脚本代码:
function Div() 中的WebService.Div(a,b,OnSucceeded,OnFailed);意思是调用web服务WebService的Div方法
function Div()
{
var a=$get("Text1").value;
var b=$get("Text2").value;
WebService.Div(a,b,OnSucceeded,OnFailed);
}
// 这是成功调用的回调函数。
function OnSucceeded(result)
{
var ReltElem=$get("Results");
ReltElem.innerHTML= result;
}
// 这是失败的回调函数。
function OnFailed(error)
{
var stackTrace=error.get_stackTrace();
var message=error.get_message();
var statusCode=error.get_statusCode();
var exceptionType=error.get_exceptionType();
var timedout=error.get_timedOut();
var ReltElem=$get("Results");
ReltElem.innerHTML=
"堆栈追踪:" + stackTrace + "<br/>" +
"服务错误:" + message + "<br/>" +
"状态码:" + statusCode + "<br/>" +
"异常类型:" + exceptionType + "<br/>" +
"超时:" + timedout;
}
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
{
var a=$get("Text1").value;
var b=$get("Text2").value;
WebService.Div(a,b,OnSucceeded,OnFailed);
}
// 这是成功调用的回调函数。
function OnSucceeded(result)
{
var ReltElem=$get("Results");
ReltElem.innerHTML= result;
}
// 这是失败的回调函数。
function OnFailed(error)
{
var stackTrace=error.get_stackTrace();
var message=error.get_message();
var statusCode=error.get_statusCode();
var exceptionType=error.get_exceptionType();
var timedout=error.get_timedOut();
var ReltElem=$get("Results");
ReltElem.innerHTML=
"堆栈追踪:" + stackTrace + "<br/>" +
"服务错误:" + message + "<br/>" +
"状态码:" + statusCode + "<br/>" +
"异常类型:" + exceptionType + "<br/>" +
"超时:" + timedout;
}
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
WebService代码:首先添加using System.Web.Script.Services;
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Web.Script.Services;
/// <summary>
/// WS_DivideByZeroWebService 的摘要描述
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
public class WS_DivideByZeroWebService : System.Web.Services.WebService
{
public WS_DivideByZeroWebService()
{
//如果使用设计的组件,请取消批注下行程序代码
//InitializeComponent();
}
[WebMethod()]
public string Div(int a, int b)
{
int division = a / b;
string result =
String.Format("相除的结果是:{0}", division.ToString());
return result;
}
}
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Web.Script.Services;
/// <summary>
/// WS_DivideByZeroWebService 的摘要描述
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
public class WS_DivideByZeroWebService : System.Web.Services.WebService
{
public WS_DivideByZeroWebService()
{
//如果使用设计的组件,请取消批注下行程序代码
//InitializeComponent();
}
[WebMethod()]
public string Div(int a, int b)
{
int division = a / b;
string result =
String.Format("相除的结果是:{0}", division.ToString());
return result;
}
}