I'm facing an issue with two files in my project - one is a PHP file containing an array that is echoed using json_encode, and the other is a JavaScript file containing various functions for a webpage. One particular function in the JavaScript file is giving me trouble as it appears to be incomplete:
/*
* 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);
}
This function is intended to process the array from the PHP file, but it is not functioning as expected. Despite the fact that the value of this.responseText
is in JSON format like this:
var jsonArr =
["1","2,","3"]
When the function is activated, nothing happens, and evaluating this.responseText
results in "undefined."
I'm struggling to figure out what I am doing wrong here. If necessary, I can provide more code examples or details about the actual array. This problem is really frustrating me.