我有一个在xampp上运行的脚本,当我在其中运行带有警报调用的脚本时,文件就会执行,但是当我在没有警报的情况下运行该脚本时,它就坐在那儿,不返回任何内容。我究竟做错了什么?

码:

var xhr;

函数getPlants(xhr){
        尝试{
            xhr = new XMLHttpRequest();
            }赶上(microsoft){
            尝试{
                xhr = new ActiveXObject(“ Msxml2.XMLHTTP”);
                }赶上(othermicrosoft){
                    尝试{
                xhr = new ActiveXObject(“ Microsoft.XMLHTTP”);
                    }捕获(失败){
                        xhr = false;
                        alert(“不支持ajax”);
                    }
                }
        }

xhr.onreadystatechange=state_change;
xhr.open("GET","db_interactions.php",true);
xhr.send(null);
    alert("sent");


function state_change() {
    if(xhr.readyState==4 && xhr.status==200) {
        alert("all systems go");
        return xhr.responseText;
    }


}

最佳答案

我强烈建议使用jQueryPrototype。它们是JavaScript库,可以在进行Ajax调用和许多其他JavaScript操作时使您的生活更轻松。

jQuery的Ajax示例:

 $.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });

10-05 20:45
查看更多