XMLHttpRequest学习笔记
2009-06-23 12:16
393 查看
var xmlHttp;
var queryStr;
function createQueryString()
{
queryStr=location.href.substring(location.href.indexOf("?"),location.href.length);
}
function doRequest()
{
createXMLHttpRequest();
createQueryString();
xmlHttp.onreadystatechange=parseResult;
xmlHttp.open("post","default.aspx"+queryStr,true);
xmlHttp.send(null);
}
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
else
alert("您的浏览器不支持XMLHttpRequest!")
}
function parseResult()
{
if(xmlHttp.readyState==4&&xmlHttp.status==200)
{
var responseDiv=document.getElementById("responseDiv");
var result=document.createTextNode(xmlHttp.responseText);
responseDiv.appendChild(result);
}
}
<input type="button" onclick="doRequest()" value="OK"/>/*点击按钮时,将传入本页的URL参数传到服务器,拼接在一起,返回。*/
default.aspx的Page_Load()代码:
string resp = null;
if (Request["uid"] != null)
resp += Request["uid"].ToString();
if (Request["pwd"] != null)
resp += Request["pwd"].ToString();
if (resp != null)
{
Response.Write(resp);
Response.End();//加上一句,XMLHttp.responseText的值才是uid+pwd,否则为html代码
}
总结:如果要获得服务器的全部返回数据就用XMLHttpRequest,要获取服务器某个方法的返回值可用webmethod
var queryStr;
function createQueryString()
{
queryStr=location.href.substring(location.href.indexOf("?"),location.href.length);
}
function doRequest()
{
createXMLHttpRequest();
createQueryString();
xmlHttp.onreadystatechange=parseResult;
xmlHttp.open("post","default.aspx"+queryStr,true);
xmlHttp.send(null);
}
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
else
alert("您的浏览器不支持XMLHttpRequest!")
}
function parseResult()
{
if(xmlHttp.readyState==4&&xmlHttp.status==200)
{
var responseDiv=document.getElementById("responseDiv");
var result=document.createTextNode(xmlHttp.responseText);
responseDiv.appendChild(result);
}
}
<input type="button" onclick="doRequest()" value="OK"/>/*点击按钮时,将传入本页的URL参数传到服务器,拼接在一起,返回。*/
default.aspx的Page_Load()代码:
string resp = null;
if (Request["uid"] != null)
resp += Request["uid"].ToString();
if (Request["pwd"] != null)
resp += Request["pwd"].ToString();
if (resp != null)
{
Response.Write(resp);
Response.End();//加上一句,XMLHttp.responseText的值才是uid+pwd,否则为html代码
}
总结:如果要获得服务器的全部返回数据就用XMLHttpRequest,要获取服务器某个方法的返回值可用webmethod
相关文章推荐
- 学习笔记 之--AJAX核心对象 XMLHttpRequest
- XMLHTTPRequest使用【学习笔记】
- AJAX 学习笔记(1) XMLHttpRequest对象概述
- ajax学习笔记:ajax原态-XmlHttpRequest
- Ajax学习笔记,原生Ajax,使用XMLHttpRequest读取xml内容
- AJax 学习笔记一(XMLHTTPRequest对象)
- ajax,json学习笔记(二)XMLHttpRequest对象
- AJAX学习笔记:创建XMLHttpRequest对象的五步骤
- JavaScript权威设计--跨域,XMLHttpRequest(简要学习笔记十九)
- jquery ajax学习笔记2 使用XMLHttpRequest对象的responseXML
- 学习笔记---使用prototype.js扩展struts标签实现xmlhttprequest
- xml,javascript,XMLHttpRequest 学习笔记
- ajax学习笔记-XMLHttpRequest对象的使用
- XMLHttpRequest学习笔记一
- Ajax学习笔记——XMLHttpRequest对象发送request
- AJax学习笔记一(XMLHTTPRequest对象)
- ajax学习笔记---XMLHttpRequest的属性和方法
- [学习笔记]XMLHttpRequest实现局部刷新