首页 > 代码库 > ajax get post

ajax get post

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=http://www.mamicode.com/‘AdminImg/list_style.gif‘/>1

1

dasfads

1

");
            else if(indexnum == "2")
                Response.Write("<p><img src=http://www.mamicode.com/‘AdminImg/list_style.gif‘/>2

2

dasfads

2

");
            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=http://www.mamicode.com/‘AdminImg/list_style.gif‘/>1

1

dasfads

1

");
            else if(indexnum == "2")
                Response.Write("<p><img src=http://www.mamicode.com/‘AdminImg/list_style.gif‘/>2

2

dasfads

2

");
            Response.End();
        }

分享: