<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="addType()" />    <script type="text/javascript" >
        function addType() {
            var getType = prompt("Please input Type", '');
            if (getType!=null) {
                var XmlHttp = createXMLHttpResponse(); 
                if (XmlHttp) { 
                    XmlHttp.onreadystatechange = interactWithServer(XmlHttp); 
                    XmlHttp.open("GET", " interactWithAJAX.aspx?action=addType&type=" + getType,true); 
                    XmlHttp.send();
                }
                else {
                    alert("XMLHttpResponse is null");
                }
            }
            else {
                alert("Type not be save");
            }
        }
        
        function createXMLHttpResponse (){ 
            if(window.ActiveXObject )
                return new ActiveXObject("Microsoft.XMLHTTP");
            else(window.XMLHttpRequest)
                return new XMLHttpRequest();    
        }        function interactWithServer(XmlHttp) { 
             if (XmlHttp.readyState == 4) { 
                 if (XmlHttp.status == 200) { 
                    var isAddToDB = XMLHttpRequest.responseText;
                    alert(isAddToDB);
                }
                else
                 {
                    alert("There are some problem on server ,and it's status is 400");
                }
            } 
        }    </script>1我第一次写ajax,但是它不去调用asp.net的后台c#代码。请大家帮帮忙,看看为什么interactWithServer(XmlHttp)不去调用后台代码呢?2 我有些不理解 :
 XmlHttp.onreadystatechange = interactWithServer(XmlHttp); //1
                    XmlHttp.open("GET", " interactWithAJAX.aspx?action=addType&type=" + getType,true); //2
                    XmlHttp.send();//3
1 不是已经连接上了服务器了吗?为什么2 open 和3 send 还在后面呢?求解AjaxJavaScript

解决方案 »

  1.   

    1、XmlHttp.onreadystatechange = function () { interactWithServer(XmlHttp); }
    2、if (XmlHttp.status == 200) {
                          var isAddToDB = XmlHttp.responseText;
                          alert(isAddToDB);
               }
    3、 interactWithAJAX.aspx?这个路径 前有个空格 不知你本地是否有
      

  2.   

    你把 true 改成false试一下
      

  3.   

    看不出来什么问题,是不是需要设置一下content-type??
      

  4.   

    http://www.cnblogs.com/hxtgirq710/p/3160865.html
    可以参考一下
    使用  SydAjax.ajax({url:'',type:'',success:function(data){}});
      

  5.   

    XmlHttp.onreadystatechange = interactWithServer(XmlHttp);
    这行代码有问题,返回值永远为0,
    注意URL是否需要加\
      

  6.   

    对!就是这两个问题。改成
    XmlHttp.onreadystatechange =function(){ interactWithServer(XmlHttp);};就好了