#Ajax GET と POST
GET
http-get.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
</head>
<body>
<script>
function get()
{
var xhr = new XMLHttpRequest();
xhr.open("get", "http://localhost/ajax/http-get.php?data=HelloKitty");
xhr.send();
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.status === 200)
{
alert(xhr.responseText);
}
}
}
get();
</script>
</body>
</html>
http-get.php
<?php
$contents = $_GET["data"];
echo $contents;
POST
http-post.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
</head>
<body>
<script>
function post()
{
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost/ajax/http-post.php');
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
xhr.send( 'data=HelloKitty' );
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.status === 200)
{
alert(xhr.responseText);
}
}
}
post();
</script>
</body>
</html>
http-get.php
<?php
$contents = $_POST["data"];
echo $contents;