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 8152259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:39:26+00:00 2026-06-06T15:39:26+00:00

I am using json to read records. It works perfectly fine if I use

  • 0

I am using json to read records. It works perfectly fine if I use it inside the javascript like this

var events=[{eventId:"1", event_name:"wedding"},{eventId:"2", event_name:"interview"}]

then loop it to read records

for(var events_count=0;events_count<events.length;events_count++)
{
//read records and works perfectly fine
}

but when I am doing the same using ajax, it is not working. I have even put the text

{eventId:"1", event_name:"wedding"},{eventId:"2", event_name:"interview"}

in the ajax called(ajax-get-events.php) page, It doesn’t go inside the loop

    var xmlhttp;
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(){
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        //got data in same format as above in xmlhttp.responseText and used eval to parse it

         events = eval('(' + xmlhttp.responseText + ')');

        //I have tried to alert(events) and it shows [object object]

           for(var events_count=0;events_count<events.length;events_count++)
            {
                //loop doesn't work at all

            }
       }
    }
    xmlhttp.open("GET","ajax-get-events.php",true);
    xmlhttp.send();

Please tell me what I am missing.

Thanks

  • 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-06-06T15:39:27+00:00Added an answer on June 6, 2026 at 3:39 pm

    This is not parsed as a JSON array, but as one object (the second one):

    {eventId:"1", event_name:"wedding"},{eventId:"2", event_name:"interview"}
    

    But this should be parsed (read eval’d) as a correct array:

    [{"eventId":"1", "event_name":"wedding"},{"eventId":"2", "event_name":"interview"}]
    

    Since you’re using PHP, use json_encode to generate your JSON output, it’ll be correct JSON.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I parse data using JSON in javascript, I find this is very convenient. But
i need to read from json like this: {error_message:Only post requests are allowed,reservations:null,error_code:1} and
I would like to use JSON, however, the security issues related to using JSON
I'm trying to read data into my calendar visualisation using JSON. At the moment
I'm trying to read a legacy JSON code using Jackson 2.0-RC3, however I'm stuck
I am using Json Result to show a table, it is working fine when
I've got a JSON value that has been converted from a JavaScript object using
I am running hive query using get_json_object to read json strings from files in
I am trying to read JSON objects using the QScriptValue class in Qt and
I'm using json.dump() and json.load() to save/read a dictionary of strings to/from disk. The

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.