您的位置:首页 > 理论基础 > 计算机网络

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: