很简单的JavaScript不会开火

时间:2014-01-02 06:05:04

标签: javascript php ajax

我正在尝试编写世界上最简单的AJAX脚本来测试PHP往返。我一定是在失去理智,因为我甚至没有从这里得到“这里”。相反,我得到的只是event.returnValue is deprecated. Please use the standard event.preventDefault() instead.,它看起来像Chrome错误,应该与此无关。有人看到我太密集了吗?感谢。

编辑:即使我在console.log("here") return false之后注释掉所有内容,我仍然无法在此处显示简单的控制台输出。 Aaugh!

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>PHP Tester</title>
</head>

<body>

    <script type="text/javascript">

        function testPHP() {

            var first = "vic";
            var second = "tory";

            console.log("here");

            var xmlhttp=new XMLHttpRequest();

            xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                    var response = xmlhttp.responseText;

                    console.log(response);
                }
            }

            var content =   "first="    +encodeURIComponent(id)+
                            "&second="  +encodeURIComponent(second);

            xmlhttp.open("POST","phpTest.php",true);
            xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
            xmlhttp.send(content);

            return false;
        }

    </script>

    <div>

        <button type="submit" onClick="testPHP()">Test PHP</button>          

    </div>

</body>

</html>

2 个答案:

答案 0 :(得分:1)

您收到undefined消息..这是因为您没有任何名为id的变量

替换

var content =   "first="    +encodeURIComponent(id)+

var content =   "first="    +encodeURIComponent(first)+
                                             ----^ // Replace the id with this first 

答案 1 :(得分:0)

重试:

<button type="button" onClick="testPHP()">Test PHP</button>   

此外,id未定义,您可能希望使用first

var content = "first=" + encodeURIComponent(first) + "&second="  + encodeURIComponent(second); 
相关问题