【问题标题】:$.post to php file in Amazon EC2 doesn't seem to work$.post 到 Amazon EC2 中的 php 文件似乎不起作用
【发布时间】:2015-03-02 07:11:18
【问题描述】:

我已将 test.html 和 test.php 文件上传到我的 AWS Amazon EC2 中的 /var/www/html

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

test.php的网址是: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";
?>

我希望得到警报(“成功”);当我点击“点击我”按钮时,我什么也没得到。

当我点击按钮时,我得到 alert("Post to PHP");。所以似乎这两个文件都在工作,除了 $.post(); 我在这里做错了什么?

【问题讨论】:

标签: javascript php ajax post amazon-ec2


【解决方案1】:
  1. 这两个 url 无法访问,我不确定你在做什么,但是将 php 文件放到网上并不意味着它可以访问并且会作为脚本运行。时间>
    URL1:http://ec2-11-11-111-11.ap-northeast-1.compute.amazonaws.com/test.html

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

  2. 包含 jQuery

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多