我在我的AWS Amazon EC2中将test.html和test.php文件上传到/ var / www / html
test.html的网址为: 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";
?>
我期待得到警觉(&#34;成功&#34;);当我点击&#34;点击我&#34;按钮,但我一无所获。
当我点击按钮时,我得到警告(&#34;发布到PHP&#34;); 。所以似乎两个文件都在工作,除了$ .post(); 我在这做错了什么?
答案 0 :(得分:0)
这两个网址无法访问,我不确定你在做什么,但将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
包含 jQuery 库
更新
如果包含jquery库,则js没有问题。我测试了它成功发送了post请求。 jsFiddle