-1

变量 xmlhttp=new XMLHttpRequest()被初始化。以下代码:

function makerequest(serverPage,objID){
            var obj=document.getElementById(objID);
            xmlhttp.open("GET",serverPage);
            xmlhttp.onreadystatechange = function(){
                if(xmlhttp.readyState == 4 && xmlhttp.status ==200){
                    obj.innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.send(null);
        }

对不起,我是ajax的新学习者,在if条件下,为什么要添加xmlhttp.readyState == 4。在函数的末尾, xmlhttp.send(null);我可以删除它们吗?谢谢你。

4

1 回答 1

0

好吧,您想发送您生成的 ajax 请求,因此由于您使用的是 get,所以 null 是一个可接受的参数。如果你使用 post,你应该在 send 方法中传递查询字符串。更多在这里

如果您删除了 readyState 条件,那么您最终可能会导致 ajax 什么也不返回,因为页面还没有准备好。在这里查看更多。

编辑: POST 发送方法的示例参数:

xmlhttp.open("POST","ajax_test.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");
于 2012-04-28T03:59:54.827 回答