读书人

Ajax新手请问初级有关问题

发布时间: 2012-05-16 11:12:12 作者: rapoo

Ajax新手请教初级问题

JScript code
<script type="text/javascript">        window.onload = function()        {            try            {                var xmlhttp = new XMLHttpRequest();            }            catch(e)            {                var xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');            }            xmlhttp.open('GET','WebForm1.aspx',true);            xmlhttp.setRequestHeader('COOKIE','author=Robin Chen');            xmlhttp.onreadystatechange=function()            {                document.body.innerHTML+='<div>readyState:'+xmlhttp.readyState+'</div>';                if(xmlhttp.readyState == 4 && xmlhttp.status == 200)                {                    document.body.innerHTML+='<div>responseText:'+xmlhttp.responseText+'</div>';                    //alert(xmlhttp.responseText);  //这句打印出来把前台页面都打印出来了                }            }            xmlhttp.send();        }    </script>

C# code
string strName = HttpContext.Current.Request.QueryString["author"];  //这个author值得不到string strRes = "This is the response from the server:\r\n" + "Hello, " + strName + "!";HttpContext.Current.Response.Clear();HttpContext.Current.Response.Write(strRes);HttpContext.Current.Response.Flush();HttpContext.Current.Response.End();

最终打印的结果应该是
readyState:1
readyState:2
readyState:3
readyState:4
responseText:Robin Chen

[解决办法]
直接用xmlhttp 兼容问题挺头痛,直接用jquery 的ajax吧,写起来方便,兼容性好,比如用你上面的例子
JScript code
    $(document).ready(function() {        $.get("WebForm1.aspx",function(data){            $("body").html(data);        });    }); 

读书人网 >Ajax

热点推荐