残念ながら再ポストする(Admins、他のものを削除してください)。 私は現在、AJAXでPHPのマネージャークラスがXmlHttpオブジェクトを介してjavascriptで通信できるようにすることを試みていますが、私はあなたに大きな助けをしてくれています。クライアント側に。しかし、私はJSON経由でクライアントに何かを送ることができますが、私はクライアント側で読むことができません。実際には、「時間」はセッションの未定義のインデックスであるというエラーが発生しています。だから私は疑問に思っていた:私は間違って何をしていますかJavascript/ajax/php質問:サーバーからクライアントへの送信が動作し、クライアントからサーバーへの送信が失敗します
Ajaxのためのjavascriptcode:
<script type="text/javascript">
var sendReq = GetXmlHttpObject();
var receiveReq = GetXmlHttpObject();
var JSONIn = 0;
var JSONOut= 0;
//var mTimer;
//function to retreive xmlHTTp object for AJAX calls (correct)
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
//Gets the new info from the server
function getUpdate() {
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
receiveReq.open("GET", "index.php?json="+JSONIn+"&sid=$this->session", true);
receiveReq.onreadystatechange = updateState;
receiveReq.send(null);
}
}
//send a message to the server.
function sendUpdate(JSONstringsend) {
JSONOut=JSONstringsend;
if (sendReq.readyState == 4 || sendReq.readyState == 0) {
sendReq.open("POST", "index.php?json="+JSONstringsend+"&sid=$this->session", true);
sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
alert(JSONstringsend);
sendReq.onreadystatechange = updateCycle;
sendReq.send(JSONstringsend);
}
}
//When data has been send, update the page.
function updateCycle() {
getUpdate();
}
function updateState() {
if (receiveReq.readyState == 4) {
// JSONANSWER gets here (correct):
var JSONtext = sendReq.responseText;
// convert received string to JavaScript object (correct)
alert(JSONtext);
var JSONobject = JSON.parse(JSONtext);
// updates date from the JSONanswer (correct):
document.getElementById("dateview").innerHTML= JSONobject.date;
}
//mTimer = setTimeout('getUpdate();',2000); //Refresh our chat in 2 seconds
}
</script>
実際にAjaxコード使う関数:私は知りません
private function handleReceivedJSon($json){
$this->jsonLocal=array();
$json=$_POST["json"];
$this->jsonDecoded= json_decode($json, true);
if(isset($this->jsonDecoded["date"])){
$_SESSION["date"]=$this->jsonDecoded["date"];
$this->useddate=$this->jsonDecoded;
}
if(isset($this->jsonDecoded["logout"])){
session_destroy();
exit("logout");
}
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Type: text/xml; charset=utf-8");
exit($json);
}