Problem with Ajax, JSon and Prototype.js in IE8
As you see in the simplified (hope I didn't break the code by simplifying) scripts below, I have setup a timer to automaticly refresh some data on my page.
The strange thing is that it works great in Chrome and FireFox, but not in IE. In IE the timer fires when it is supposed to, but the data stays the same. Putting an alert box with the string received form the server shows the old data.
Also wired, when I start a new IE session the web-page gets updated with the new data, but only one time.
What am I doing wrong?
Any help is greatly appreciated!
Gunnar.
Java Script – Client Side:
PHP Script – Server Side:
As you see in the simplified (hope I didn't break the code by simplifying) scripts below, I have setup a timer to automaticly refresh some data on my page.
The strange thing is that it works great in Chrome and FireFox, but not in IE. In IE the timer fires when it is supposed to, but the data stays the same. Putting an alert box with the string received form the server shows the old data.
Also wired, when I start a new IE session the web-page gets updated with the new data, but only one time.
What am I doing wrong?
Any help is greatly appreciated!
Gunnar.
Java Script – Client Side:
Code:
function Class_AutoLoader() {
var request=null;
this.InitializeAutoRefresh = function() {
o_RefreshTimer = new PeriodicalExecuter(GetData, 2)
}
var GetData = function() {
var cmd = '[URL unfurl="true"]http://(url)/PageData.php?Type=2&ID=12345';[/URL]
request=null;
request = getHTTPObject();
request.onreadystatechange = sendData;
request.open("GET", cmd, true);
request.send(null);
};
function sendData() {
var JSONtext = '';
var JSONobject = '';
// if request object received response
if(request.readyState == 4) {
// parser.php response
JSONtext = request.responseText;
// convert received string to JavaScript object
JSONobject = JSON.parse(JSONtext);
// notice how variables are used
for(id in JSONobject) {
if(id=="Time") {
document.getElementById('test1').innerHTML = JSONobject[id]
} else if (id=="User_Data") {
for(id_Level2 in id) {
if(id_Level2=="Customer") {
document.getElementById('test6').innerHTML = JSONobject[id_Level2]
} else if (id_Level2=="Price") {
document.getElementById('test7').innerHTML = JSONobject[id_Level2]
} else if (id_Level2=="Time") {
document.getElementById('test9').innerHTML = JSONobject[id_Level2]
}
}
}
}
id = null;
a_Result = null;
cmd = null;
}
}
function formatIntoHHMMSS(secsIn) {
//get minutes:
var minutes=parseInt(secsIn/60);
//shrink:
seconds = (secsIn%60);
//get hours:
var hours=parseInt(minutes/60);
//shrink:
minutes = (minutes%60);
//build text:
return (AddZero(hours) + ":" + AddZero(minutes) + ":" + AddZero(seconds));
}
function AddZero(num) {
return ((num >= 0)&&(num < 10))?"0"+num:num+"";
}
function getHTTPObject() {
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
xhr = false;
}
}
}
return xhr;
}
o_Loader = new Class_AutoLoader ();
document.observe("dom:loaded", o_Loader.InitializeAutoRefresh);
PHP Script – Server Side:
Code:
<?php
// -----------------------------------------------------------------------------
// get the passed info
$c_ID = $_GET['ID'];
$n_Type = $_GET['Type'];
Function GetData($c_ID, &$a_ResultData, $o_DB=NULL) {
$b_Unset_o_DB = FALSE;
if (is_null($o_DB)) {
$o_DB = OpenDB();
$b_Unset_o_DB = TRUE;
}
$a_ResultData = array(
'Customer' => '1111111',
'Price' => '2222222',
'Time' => time());
//
if ($b_Unset_o_DB == TRUE) {
CloseDB($o_DB);
unset($o_DB);
}
}
function GetStatus($c_ID, $o_DB=NULL) {
$b_Unset_o_DB = FALSE;
if (is_null($o_DB)) {
$o_DB = OpenDB();
$b_Unset_o_DB = TRUE;
}
GetData($c_ID, $JSON_array, $o_DB);
// finalize the variable to be returned
//$a_TempStr .= '}';
$JSON_response = json_encode($JSON_array);
//
if ($b_Unset_o_DB == TRUE) {
CloseDB($o_DB);
unset($o_DB);
}
return $JSON_response;
}
if ($n_Type == '2') {
// refresh page
$a_ResultData = GetStatus($c_ID, $n_LastPrice);
echo $a_ResultData;
}