どこでも検索しましたが、この問題の答えを見つけることができません。PHPでajax投稿要求にアクセスできない
少しのajaxスクリプトを書いていますが、POSTリクエストの正しい値を取得できません。 これは、これまでのコードです:
<textarea id="message" name="message" style="width:100%;"></textarea>
<input value="SEND" style="border-radius: 5px 5px 5px 5px;" type = 'button' onclick = 'ajaxFunction()'/>
<script type="text/javascript"> <!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data
// sent from the server and will update
// div section in the same page.
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('chbox');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Now get the value from user and pass it to
// server script.
var message = document.getElementById('message').value;
var queryString = message ;
ajaxRequest.open("POST", 'chatdata.php', true);
//ajaxRequest.send(null);
ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
ajaxRequest.send('queryString');
}
</script>
<?php
$message1 = $_REQUEST['message'];
echo $message;
?>
私はこれは私が配列([のqueryString] =>)得るものですPOST値 の内容を確認するためにprint_r($message);
を使用しています。値はありません。 私のコードで何が間違っている可能性がありますか? (私はjQueryのを使用しているだろうが、私はよく、まだそれに接地していないよ)
を。 [この回答](http://stackoverflow.com/a/9713078)を参照してください。 –
たぶん 'var queryString =" queryString = "+ message;'?ポストデータは、 'key1 = value1&key2 = value2&key3 = value3&...'のような一連の 'key = value'ペアであることに注意してください。 –
イベントを定義するのではなく、XMLHttpRequestオブジェクトを使用するイベントハンドラを定義すると有益ですXMLHttpRequestオブジェクトを直接インスタンス化するハンドラです。 –