简体   繁体   English

非常简单的javascript不会触发

[英]Very simple javascript won't fire

I'm trying to write the world's simplest AJAX script just to test a PHP round trip. 我正在尝试编写世界上最简单的AJAX脚本,只是为了测试PHP往返。 I must be losing my mind because I'm not even getting the "here" to come out of this thing. 我一定失去了理智,因为我什至没有从这件事中得到“这里”。 Instead, all I get is event.returnValue is deprecated. Please use the standard event.preventDefault() instead. 相反,我得到的只是event.returnValue is deprecated. Please use the standard event.preventDefault() instead. event.returnValue is deprecated. Please use the standard event.preventDefault() instead. which looks like a Chrome bug that should have nothing to do with this. 看起来像一个Chrome错误,应该与此无关。 Anyone see what I am too dense to see? 有人看到我太密集了吗? Thanks. 谢谢。

EDIT: Even if I comment out everything after console.log("here") all the way through return false , I still don't get the simple console output "here" to appear. 编辑:即使我注释掉console.log("here")所有内容,直到return false ,我仍然不会出现简单的控制台输出“ here”。 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>

You are getting an undefined message .. that is because you don't have any variable called id 您收到一条undefined消息..那是因为您没有任何名为id变量

Replace 更换

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

with

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

Retry with: 重试:

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

Also id is undefined maybe you want to use first : id是不确定的,也许你想用first

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM