$.post-to-php文件在AmazonEC2中没有';似乎不起作用


$.post to php file in Amazon EC2 doesn't seem to work

我有test.html和test.php文件上传到我的AWS亚马逊EC2 中的/var/www/html

test.html的URL为:http://ec2-11-11-111-11.ap-northeast-1.compute.amazonaws.com/test.html

test.php的URL为:http://ec2-11-11-111-11.ap-northeast-1.compute.amazonaws.com/test.php

test.html是:

<!DOCTYPE html>
<head>
    <script type="text/javascript" charset="utf-8">
        function myFunction(){
            var url = "http://ec2-11-11-111-11.ap-northeast-1.compute.amazonaws.com/test.php";
            var data = "test_data_string"; 
            alert("Post to PHP"); //check to see if javascript is working
            $.post(url, {testdata:data},
                        function(echo){alert(echo);});
            }
    </script>
</head>
<body>
    <button onclick="myFunction()">Click me</button>
</body>
</html>

test.php是:

<?php
   $data = $_POST['testdata'];
   echo "success";
?>

我期望得到警觉("成功");当我点击"点击我"按钮时,却一无所获。

我得到警报("发布到PHP")当我点击按钮时。因此,除了$.post();我在这里做错了什么??

  1. 这两个url不可访问,我不确定你在做什么,但将php文件联机并不意味着它可以访问并将作为脚本运行
    URL1:http://ec2-11-11-111-11.ap-northeast-1.compute.amazonaws.com/test.html

    URL2:http://ec2-11-11-111-11.ap-northeast-1.compute.amazonaws.com/test.php

  2. 包括jQuery

更新:
如果您包含jquery库,那么您的js就没有问题。我测试了它成功发送帖子请求。jsFiddle