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??
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.