会员
众包
新闻
博问
闪存
云市场
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
Just do it...
学而不思则罔,思而不学则殆...
博客园
首页
新随笔
联系
管理
订阅
Basic of Ajax
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
CallWebService.aspx.cs
"
Inherits
=
"
CallWebService
"
%>
<!
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
>
Untitled Page
</
title
>
</
head
>
<
body
>
<
script
language
="javascript"
>
var
xmlhttp
=
false
;
try
{
xmlhttp
=
new
ActiveXObject(
"
Msxml2.XMLHTTP
"
);
}
catch
(e)
{
try
{
xmlhttp
=
new
ActiveXObject(
"
Microsoft.XMLHTTP
"
);
}
catch
(e)
{
xmlhttp
=
false
;
}
}
if
(
!
xmlhttp
&&
typeof
xmlhttp
!=
'
undefined
'
)
{
try
{
xmlhttp
=
new
XMLHttpRequest();
}
catch
(e)
{
xmlhttp
=
false
;
}
}
function
RequestByGet()
{
//
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//
to call webservice
var
URL
=
"
http://localhost/webservicetest/service.asmx/GetDataSet?name=
"
+
document.getElementById(
"
txt
"
).value;
xmlhttp.Open(
"
GET
"
,URL,
true
);
xmlhttp.onreadystatechange
=
updatePage;
xmlhttp.Send(
null
);
//
var result = xmlhttp.status;
//
if(result == 200)
//
document.write(xmlhttp.responseText);
//
xmlhttp = null;
}
function
RequestByPost()
{
var
strPara
=
"
name=
"
+
document.getElementById(
"
txt
"
).value;
//
to call general .aspx file
var
URL
=
"
BeCalled.aspx
"
;
xmlhttp.Open(
"
POST
"
,URL,
false
);
xmlhttp.SetRequestHeader(
"
Content-Type
"
,
"
application/x-www-form-urlencoded
"
);
xmlhttp.SetRequestHeader(
"
Content-Length
"
,strPara.length);
xmlhttp.Send(strPara);
var
result
=
xmlhttp.status;
if
(result
==
200
)
document.write(xmlhttp.responseText);
else
alert(result);
xmlhttp
=
null
;
}
function
updatePage()
{
if
(xmlhttp.readyState
==
4
)
{
document.getElementById(
"
showstatus
"
).innerText
=
""
;
if
(xmlhttp.status
==
200
)
{
//
alert(xmlhttp.responseXML.xml);
document.getElementById(
"
age
"
).value
=
xmlhttp.responseXML.selectSingleNode(
"
//diffgr:diffgram/NewDataSet/Table1/b
"
).text;
}
else
if
(xmlhttp.status
==
404
)
alert(
"
Requested URL is not found.
"
);
else
if
(xmlhttp.status
==
403
)
alert(
"
Access denied.
"
);
else
alert(
"
error, status code:
"
+
xmlhttp.status);
}
else
document.getElementById(
"
showstatus
"
).innerText
=
"
loading
"
;
}
</
script
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
div
id
="showstatus"
></
div
>
<
input
type
="text"
id
="txt"
onchange
="RequestByGet()"
/>
<!--
<input type="button" value="CallWebServiceByGet" onclick="RequestByGet()" />
-->
<
input
type
="button"
value
="CallWebServiceByPost"
onclick
="RequestByPost()"
/>
<
br
/>
<
input
type
="text"
id
="age"
/>
</
div
>
</
form
>
</
body
>
</
html
>
posted @
2008-01-18 16:35
Ready!
阅读(
203
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告