很抱歉重新发布(管理员,请删除另一个!)。 既然你们得到了很大的帮助,我有点希望你能再次帮助我,同时提出以下问题: 我目前正在尝试使用AJAX,允许PHP中的managerclass通过XmlHttpobject与客户端上的javascript进行通信。但是,我可以通过JSON向客户端发送内容,但我无法在客户端读取它。事实上,我收到的错误是“时间”是Session中未定义的索引。所以我想知道:我做错了什么?
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代码的函数:
//datepickerdata
$(document).ready(function(){
$("#datepicker").datepicker({
onSelect: function(dateText){
var JSONObject = {"date": dateText};
var JSONstring = JSON.stringify(JSONObject);
sendUpdate(JSONstring);
},
dateFormat: 'dd-mm-yy'
});
});
</script>
PHP代码:
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);
}
答案 0 :(得分:0)
我不知道我是不对的。但似乎您在不实现JQuery的情况下使用JQuery命令。如果是这样,那么你的ajax命令就没用了。
$(document).ready(function(){
....
});
典型的JQuery函数。
答案 1 :(得分:0)
这里只是一个问题,您是否将遗留应用移植到jquery?
我首先要在这里删除“Ajax”调用,以便在jquery
中清除它$(...).ajax(...)
或原型
ajax = new Ajax.Request(...)
然后当响应回来时,一定要解析/评估`
response.responseText
response.responseText.evalJSON()
等等......