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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:54:47+00:00 2026-05-27T20:54:47+00:00

I am echoing JSON from a php server back to the browser. I am

  • 0

I am echoing JSON from a php server back to the browser. I am quiet proficient with XML however am new to JSON. Can someone show me how to correctly extract the JSON from the xmlhttpRequest and then pass it into data aka an alert.

My JSON (from PHP server)

       {"data": {
       "message": "Open the Pod bay doors, Hal",
       "type": "request",           
       "replies" => array(
                 "id": "12321"
                 "message": "I'm sorry Dave, I'm afraid I can't do that!"
        )
        }
        }

My request in JS returns the JSON however I have no way of catching it or extracting the inside information… my ajax function is…

function ajax(site){  
    xmlhttp.open("GET","site",true);
    xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
       if (xmlhttp.status!=404) {
       var resp =new Function("return "+xmlhttp.responseText)();

     }
   }
 xmlhttp.send(null);
 }

Then I am invoking the function in window.onload

    window.onload = runJSON()

function runJSON(){
    var site = "http://localhost/sites/sandbox/json.php"
    ajax(site);
    ... this is what i am unsure about... how do I access the data in the object
}
alert(data);
}
  • 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-27T20:54:48+00:00Added an answer on May 27, 2026 at 8:54 pm
    "replies" => array(
                 "id": "12321"
                 "message": "I'm sorry Dave, I'm afraid I can't do that!"
        )
    

    needs to be

    "replies": {
      "id": "12321",
      "message": "I'm sorry Dave, I'm afraid I can't do that!"
    }
    

    in JavaScript, you can convert JSON to a JS object by running JSON.parse(str), where you should get your str from a callback from inside the onreadystatechange handler:

    if (xmlhttp.status!=404) {
      var resp =new Function("return "+xmlhttp.responseText)();
      ajaxDone(resp);
    }
    function ajaxDone(str) {
      try {
        var data = JSON.parse(str);
        console.log(data);
      } catch (x) {
        // TODO: see what happened
      }
    }
    

    Note: JSON.parse does not exist in older browsers. If you wish to support them too, you will need a library that will shim them in, like jQuery (which will incidentally also take away a lot of pain in AJAX department).

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

Sidebar

Related Questions

Suppose I am echoing random data from PHP to browser. Total amount of random
I have a php loop that is echoing out geolocation values. How can I
I've got a php file echoing hashes from a MySQL database. This is necessary
So from my php, I am echoing {error:1}: $(document).ready(function(){ $('#form').submit(function(e){ register(); }); }); function
Here is how I am echoing back a 'normal' table cell in PHP: Code:
I am 'echoing' a json array back to ajax, but it is printing the
HI im having a little difficulty with dataTables and php. I'm echoing out json
Using PHP echoing the date correctly displays: 2012-01-04 11:04:07 However, at the top of
I am echoing images out on a page using php but the images and
I am echoing back these 2 variables in to a table, but wanted to

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.