首页 > 技术文章 > ajax get post

lh123 2014-07-17 23:53 原文

AJAX,get,post传参,readyState一直为0的原因

 (2012-11-18 16:33:12)
标签: 

it

 

原因

 

控件

 

编码

 

浏览器

分类: AJAX

        var xmlhttp;
        var htmltext;
        //通过浏览器的兼容性,获取"XMLHTTP"控件
        function getXMLRequster() {
            try {
                if (window.ActiveXObject) {
                    for (var i = 5; i > -1; i--) {
                        try {
                            if (i == 2)
                                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                            else
                                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP." + i + ".0");
                            break;
                        }
                        catch (e) {
                            xmlhttp = false;
                        }
                    }
                }
                else if (window.XMLHttpRequest) {
                    xmlhttp = new XMLHttpRequest();
                }
            }
            catch (e) {
                xmlhttp = false;
            }
        };
        //通过浏览器的兼容性,获取"XMLHTTP"控件
        //发送给后台
        function databand(indexnum) {
            getXMLRequster();

//问题所在


            xmlhttp.onreadystatechange = xmlhttp_onreadystatechange;

 

 

//(xmlhttp.onreadystatechange = xmlhttp_onreadystatechange();这样写的话

//xmlhttp.onreadystatechange为将xmlhttp_onreadystatechange的返回值赋给

//xmlhttp.onreadystatechange;而xmlhttp.onreadystatechange为当状态改变时调

//用函数xmlhttp_onreadystatechange所以应该这样写

//xmlhttp.onreadystatechange=xmlhttp_onreadystatechange;如果有参数的话这样//写:

//xmlhttp.onreadystatechange = functiong (){xmlhttp_onreadystatechange//(a,b);};)


            xmlhttp.open("GET", "Left.aspx?id=" + escape(indexnum), true); //get方法的escape编码
            xmlhttp.send(null); //get方法的

 

            xmlhttp.open("POST", "Left.aspx", true);//post方法
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //post方法,post必备的一句话
            xmlhttp.send("id=" + indexnum); //post方法


        }
        //发送给后台
        //判断回送
        function xmlhttp_onreadystatechange() {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    htmltext = xmlhttp.responseText;
                }
                else {
                }
            }
            else {
            }
        }
        //判断回送

//后台

//POST方法用Request.Form["id"]

        if (Request.Form["id"] != null)
        {
            string indexnum = Server.UrlDecode(Request.Form["id"]);
            if(indexnum == "1")
            Response.Write("<p><img src='AdminImg/list_style.gif'/><a href='#'>1</a></p><p><img src='AdminImg/list_style.gif'/><a href='#'>1</a></p><p><img src='AdminImg/list_style.gif'/><a href='#'>dasfads</a></p><p><img src='AdminImg/list_style.gif'/><a href='#'>1</a></p>");
            else if(indexnum == "2")
                Response.Write("<p><img src='AdminImg/list_style.gif'/><a href='#'>2</a></p><p><img src='AdminImg/list_style.gif'/><a href='#'>2</a></p><p><img src='AdminImg/list_style.gif'/><a href='#'>dasfads</a></p><p><img src='AdminImg/list_style.gif'/><a href='#'>2</a></p>");
            Response.End();
        }

//GET方法用 Request.QueryString["id"]

if (Request.QueryString["id"] != null)
        {
            string indexnum = Server.UrlDecode(Request.QueryString["id"]);
            if(indexnum == "1")
            Response.Write("<p><img src='AdminImg/list_style.gif'/><a href='#'>1</a></p><p><img src='AdminImg/list_style.gif'/><a href='#'>1</a></p><p><img src='AdminImg/list_style.gif'/><a href='#'>dasfads</a></p><p><img src='AdminImg/list_style.gif'/><a href='#'>1</a></p>");
            else if(indexnum == "2")
                Response.Write("<p><img src='AdminImg/list_style.gif'/><a href='#'>2</a></p><p><img src='AdminImg/list_style.gif'/><a href='#'>2</a></p><p><img src='AdminImg/list_style.gif'/><a href='#'>dasfads</a></p><p><img src='AdminImg/list_style.gif'/><a href='#'>2</a></p>");
            Response.End();
        }

推荐阅读