duandian8110 2015-03-02 07:11
浏览 49
已采纳

Amazon EC2中的$ .post到php文件似乎不起作用

i have test.html and test.php file uploaded to /var/www/html in my AWS Amazon EC2

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

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

test.html is:

<!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 is:

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

I am expecting to get alert("success"); when I click the "click me" button, but I am getting nothing.

i get alert("Post to PHP"); when I click on the button. so it seems that both of the files are working except $.post(); What am i doing wrong here??

  • 写回答

1条回答 默认 最新

  • douce1368 2015-03-02 07:33
    关注
    1. These two urls are not accessible , I am not sure what you are doing but putting the php file online does not mean that it is accessible and will run as a script.
      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. Include jQuery library

    update:
    Your js does not have problem if you include the jquery library. I tested it sends the post request successfully. jsFiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本