Amazon EC2 中的 $.post 到 php 文件似乎不起作用
$.post to php file in Amazon EC2 doesn't seem to work
我已将 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 的 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";
?>
我期待收到警报("success");当我点击 "click me" 按钮时,我什么也没得到。
当我点击按钮时,我得到 alert("Post to PHP");。所以看起来这两个文件都在工作,除了 $.post();
我在这里做错了什么?
这两个网址无法访问,我不确定你在做什么,但是将php文件放到网上并不意味着它可以访问并且将 运行 作为脚本。
网址 1: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
包括jQuery库
更新:
如果你包含 jquery 库,你的 js 没有问题。我测试它成功发送 post 请求。 jsFiddle
我已将 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 的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";
?>
我期待收到警报("success");当我点击 "click me" 按钮时,我什么也没得到。
当我点击按钮时,我得到 alert("Post to PHP");。所以看起来这两个文件都在工作,除了 $.post(); 我在这里做错了什么?
这两个网址无法访问,我不确定你在做什么,但是将php文件放到网上并不意味着它可以访问并且将 运行 作为脚本。
网址 1:http://ec2-11-11-111-11.ap-northeast-1.compute.amazonaws.com/test.htmlURL2: http://ec2-11-11-111-11.ap-northeast-1.compute.amazonaws.com/test.php
包括jQuery库
更新:
如果你包含 jquery 库,你的 js 没有问题。我测试它成功发送 post 请求。 jsFiddle