抱歉重新发帖(管理员,请删除另一张!)。 因为你们给了我很大的帮助,我有点希望你们能再次帮助我,同时有以下问题: 我目前正在尝试通过允许 PHP 中的管理器类通过 XmlHttpobject 与客户端的 javascript 进行通信来使用 AJAX。但是,我可以通过 JSON 向客户端发送一些内容,但我无法在客户端读取它。事实上,我收到的错误是“时间”是 session 中 undefined index 。所以我想知道:我做错了什么?
Ajax 的 JavaScript 代码:
<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);
}
最佳答案
这里只是一个问题,您是否正在将旧版应用程序移植到 jquery?
我首先要摆脱这里的“Ajax”调用,以在 jquery 中清理它
$(...).ajax(...)
或原型(prototype)
ajax = new Ajax.Request(...)
然后当响应返回时,请务必解析/评估`
response.responseText
response.responseText.evalJSON()
等等...
关于php - Javascript/ajax/php 问题 : sending from server to client works, 从客户端发送到服务器失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/705903/