0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ajax通信 GET POST

Posted at

#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;
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?