I have two files, one containing an array in PHP that is echoed via json_encode, and the other full of javascript functions for a webpage. One such function, that looks like this (and is unfinished):
/*
* Function: selectVictim
* Called from function laserOn()
*
* Selects a random victim from a list of victims
*
* @return String: victim
*/
function selectVictim()
{
var params = "url=queenofsheep.com/Sheep/victims.php";
var request = new ajaxRequest();
request.open("POST", "victims.php", true);
request.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
request.setRequestHeader("Content-Length", params.length);
request.setRequestHeader("Connection", "close");
request.onreadystatechange = function ()
{
if (this.readyState == 4)
{
if (this.status == 200)
{
if (this.responseText != null )
{
var vicString = this.responseText;
var vicArray = eval('"'+vicString+'"');
//var vicArray = vicString.split(',');
//var numVic = Math.floor(Math.random() * (vicArray - 1));
alert(vicArray);
}
else alert("Ajax error: No data received");
}
else alert("Ajax Error: " + this.statusText);
}
}
request.send(params);
}
Is supposed to take in the array from the other file and do things to it that are beyond the scope of this question. Unfortunately, while this.responseText contains a JSON encoded array of the format
var jsonArr =
["1","2,","3"]
activating the function does nothing, and evaling this.responseText yields “undefined.”
What am I doing wrong here? I can provide more examples of the real code, the actual array, whatever, if need be. This is driving me crazy.
Try adding “(” and “)” to the Eval Function. That’s how I’ve seen it done in the past