ajax 笔记--调用WebService实现求两数之和
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
xmlHttp = false
if(window.XMLHttpRequest)//开始初始化XMLHttpRequest对象//Mozilla浏览器
{
xmlHttp = new XMLHttpRequest();
if(xmlHttp.overrideMimeType) //设置Mime类别
{
xmlHttp.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Micosoft.XMLHTTP");
}
catch(e)
{
}
}
}
if(!xmlHttp)
{
//异常,创建实例失败
window.alert("不能创建XMLHttpRequest对象实例");
return false;
}
}
function TotalURL(aa,bb)
{
createXMLHttpRequest();
xmlHttp.open("POST",aa,true);
xmlHttp.onreadystatechange = processRequest;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(bb);
}
//处理返回信息的函数
function processRequest()
{
if(xmlHttp.readyState == 4)//判断对象状态
{
if(xmlHttp.status == 200)//信息已经成功返回,开始处理信息
{
var disp = document.getElementById("spnDetailDisplay");
var xmlDoc = xmlHttp.responseXML
var result = xmlDoc.lastChild.childNodes[0].nodeValue;
document.getElementById("total").innerHTML = result;
//alert(xmlHttp.responseText);
}
else
{
alert(xmlHttp.responseText);
//页面不正常
alert("你所请求的页面有异常");
}
}
}
function ExeWebService()
{
var val1 = document.getElementById("value1").value;
var val2 = document.getElementById("value2").value;
var url = "http://localhost/LiaoHaiBing/Total.asmx/ReturnTotal"
var values = "arg1=" + val1 +"&arg2="+val2;
TotalURL(url,values);
}
</script>
var xmlHttp;
function createXMLHttpRequest()
{
xmlHttp = false
if(window.XMLHttpRequest)//开始初始化XMLHttpRequest对象//Mozilla浏览器
{
xmlHttp = new XMLHttpRequest();
if(xmlHttp.overrideMimeType) //设置Mime类别
{
xmlHttp.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Micosoft.XMLHTTP");
}
catch(e)
{
}
}
}
if(!xmlHttp)
{
//异常,创建实例失败
window.alert("不能创建XMLHttpRequest对象实例");
return false;
}
}
function TotalURL(aa,bb)
{
createXMLHttpRequest();
xmlHttp.open("POST",aa,true);
xmlHttp.onreadystatechange = processRequest;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(bb);
}
//处理返回信息的函数
function processRequest()
{
if(xmlHttp.readyState == 4)//判断对象状态
{
if(xmlHttp.status == 200)//信息已经成功返回,开始处理信息
{
var disp = document.getElementById("spnDetailDisplay");
var xmlDoc = xmlHttp.responseXML
var result = xmlDoc.lastChild.childNodes[0].nodeValue;
document.getElementById("total").innerHTML = result;
//alert(xmlHttp.responseText);
}
else
{
alert(xmlHttp.responseText);
//页面不正常
alert("你所请求的页面有异常");
}
}
}
function ExeWebService()
{
var val1 = document.getElementById("value1").value;
var val2 = document.getElementById("value2").value;
var url = "http://localhost/LiaoHaiBing/Total.asmx/ReturnTotal"
var values = "arg1=" + val1 +"&arg2="+val2;
TotalURL(url,values);
}
</script>
上面代码是javascript脚本.
-------------
下面是Total.aspx的部分html代码.
<div>
<table>
<tr>
<td>第一个值:</td>
<td><input type="text" id="value1"></td>
</tr>
<tr>
<td>第二个值:</td>
<td><input type="text" id="value2"></td>
</tr>
<tr>
<td><input value="求和" type="button" onclick="ExeWebService();"></td>
<td>
<asp:Label id="lblTotal" runat="server"></asp:Label></td>
</tr>
</table>
<div id="total">
</div>
</div>
<table>
<tr>
<td>第一个值:</td>
<td><input type="text" id="value1"></td>
</tr>
<tr>
<td>第二个值:</td>
<td><input type="text" id="value2"></td>
</tr>
<tr>
<td><input value="求和" type="button" onclick="ExeWebService();"></td>
<td>
<asp:Label id="lblTotal" runat="server"></asp:Label></td>
</tr>
</table>
<div id="total">
</div>
</div>
---------------------
上面是添加一个WebServic,名字叫Total.asmx,完了之后添加如下代码放到 Total.asmx 代码里去.
/// <summary>
/// 返回两数之和
/// </summary>
/// <param name="arg1">传来的第一个参数</param>
/// <param name="arg2">传来的第二个参数</param>
/// <returns>ReturnTotal</returns>
[WebMethod]
public int ReturnTotal(int arg1,int arg2)
{
return arg1 + arg2;
}
/// 返回两数之和
/// </summary>
/// <param name="arg1">传来的第一个参数</param>
/// <param name="arg2">传来的第二个参数</param>
/// <returns>ReturnTotal</returns>
[WebMethod]
public int ReturnTotal(int arg1,int arg2)
{
return arg1 + arg2;
}