Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1025081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:52:30+00:00 2026-05-16T11:52:30+00:00

I finally succeeded in using Ajax to get SOMETHING sent from one page to

  • 0

I finally succeeded in using Ajax to get SOMETHING sent from one page to another! What I’m trying to do is pass an array from a PHP file to a Javascript file, and the Javascript file is receiving this in this.responseText:

<html>
<head>

    <script type="text/javascript">
        var jsonArray = ["chickens","horses","cows","werewolves","zombies","vampires","phantoms","U.S. Congressmen","performance artists","pieces of fencepost","barnhouses","robots","cyborgs"]

    </script>
</head>
</html>

I tried running eval() and alerting the result, but no result appears. How do I successfully extract the array from this.responseText?

Edit Here is my function thus far:

/*
 * 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 vicArray = eval('('+this.responseText+')');
                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);
}    

Second Edit The file containing the array (in PHP) is as follows:

<html>
<head>
<?php

$victims = array(

    // Animals
    "chickens",
    "horses",
    "cows",

    // Supernatural
    "werewolves",
    "zombies",
    "vampires",
    "phantoms",

    // Human
    "U.S. Congressmen",
    "performance artists",

    // Inanimate, non-mechanical
    "pieces of fencepost",
    "barnhouses",

    // Mechanical
    "robots",
    "cyborgs"

);

?>

    <script type="text/javascript">
        var jsonArray = <?php echo json_encode($victims); ?>


    </script>
</head>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:52:31+00:00Added an answer on May 16, 2026 at 11:52 am

    If your php page is returning all the text that you reported (with <html> etc…) then your output is not a JSON object, but an html page. Your response should contain only your serialized JSON object (and the proper http response headers)…

    Once you have ‘cleaned’ your output you can use JSON2 library to parse your object:
    http://www.json.org/js.html

    var myObject = JSON.parse(myJSONtext);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm generating a PDF using JasperReports from Java. I finally succeeded in making Jasper
Finally I have decided to get rid of aptana from eclipse but the Software
I'm trying to launch another process from a service (it's a console app that
Finally i tried to draw a line using OpenGL ES framework in XCode 4.2
Finally deployed my ruby on rails 3.2 app but when trying to open it
Finally figured it out thanks to One Mad Monkey, but forgot quotes on my
Finally I managed to get pylucene working on my windows7 machine, Which raised a
I finally let windows nag me into upgrading from IE6 to 8. Now in
I finally got my permalinks working for Custom Post Types and Taxonomies using `
I have a .NET WeBService project and a reference to it from another project.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.