私は、私は、このHTMLコードを持っているAJAX、 を学んでいます:それは、私が欲しいものAJAXリクエストが実行されるかどうかを知る方法?
<?php
echo "hello";
?>
私は送信ボタンをクリックしたときにということである。
<!DOCTYPE html>
<html>
<head>
<title>
My first chat page
</title>
<meta charset = "utf-8">
<style>
#message-area {
position: fixed;
bottom: 0;
width: 100%;
}
#send-button {
float: right;
margin: 20px;
color: white;
background-color: green;
}
textarea {
float: left;
width: 90%;
height: 4em;
font-family: cursive;
fon-size: 20px;
resize: none;
}
</style>
</head>
<body>
<div id="message-area">
<textarea id="myTextarea" >
</textarea>
<button type="button" id="send-button">
Send
</button>
</div>
</body>
<script>
var sendButton = document.getElementById("send-button");
function trial(){
var xhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
sendButton.innerHTML = this.responseText;
}
xhttp.open("GET", "insertmessage.php", true);
xhttp.send();
}
}
sendButton.addEventListener("click", trial);
</script>
</html>
をそして、私はこの単純なPHPコードを持っていることAJAX接続を設定し、サーバーに接続し、サーバーは「Hello」を送信し、送信ボタンの内部HTMLを変更します。私はWAMP wwwディレクトリ内の同じフォルダにそれらを試しましたが、うまくいきませんでした。
あなたの 'xmlhttp'はどこから来たのですか? –
ブラウザのインスペクタを開いて、ネットワークタブをチェックしてリクエスト結果を確認することができます –
まず試行後にsendButton.addEventListener( "click"、trial)と言う必要があります。 – mhodges