这段代码有什么问题?

时间:2010-11-22 14:52:36

标签: php javascript parameter-passing

我必须使用这段代码在页面加载时在表格中保存一些值。搜索值的函数在php中。我必须在脚本中使用这些值。我正在尝试这种方式,但它不起作用。

<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>

<script>
    $(document).ready(function() 
    {
      var msg_top = new array();
      msg_top = "<"+"?php echo searchResults('windows');"+"?"+">";
      var url = "msg2_mysql.php"
    var request = null;
        if (window.XMLHttpRequest)
             {// code for IE7+, Firefox, Chrome, Opera, Safari
               request=new XMLHttpRequest();
             }
          else
             {// code for IE6, IE5
               request=new ActiveXObject("Microsoft.XMLHTTP");
             }
    request.open("POST", url, true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Connection", "close");
    request.onreadystatechange = function(){
             if (request.readyState == 4) {
                     if (request.status == 200) {
                         alert('POST');
                     } else {
                          alert(request.status); //
                     }
                    }
                   }
      request.send("msg_top=" + encodeURIComponent(msg_top).replace(/%20/g, '+'));
    });
    </script>

1 个答案:

答案 0 :(得分:1)

if (request.readyState == 4) {
        if (request.status == 200) {
            //alert('POST');
    } else {
        alert(request.status); // fails here
    }}

应该是这样的:

    if (request.readyState == 4) {
        if (request.status == 200) {
                //alert('POST');
        } else {
            alert(request.status); // fails here
        }
    }
}

如果你正确地注意缩进,你会注意到你永远不会结束你在下面创建的匿名函数:

request.onreadystatechange = function(){