0

I am having trouble using the json object echoed from php to javascript. In the php file I define

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
echo($json);

and then in javascript file I want to access this object.

$("#test_btn").click(function() {
                $.get("serverside.php", function(data, status) { 
                   console.log("data " , data["a"]); //undefined
                   console.log(JSON.parse(data)); // error
                });
            });

I get undefined for data["a"] and an error for JSON.parse. How should I use the returend data?

8
  • 5
    Use an associative array and echo it with json_encode. Don't create your own JSON, it's prone to errors. Commented May 15, 2014 at 22:16
  • 4
    Try just console.log(data) and see what it outputs. You're probably not getting valid JSON Commented May 15, 2014 at 22:20
  • 1
    Which error are you getting? The error message usually helps. Commented May 15, 2014 at 22:23
  • 1
    I think I realized the problem, yet I don't know how to solve it! in the php file, I am reading the content of several json files, so I loop through all the files and echo in the loop. I thought the content of each file comes separately but accually what I get is a "{"a":1,"b":2,"c":3,"d":4,"e":5}{"a":1,"b":2,"c":3,"d":4,"e":5}{"a":1,"b":2,"c":3,"d":4,"e":5}..." which is not valid json Commented May 15, 2014 at 22:26
  • 1
    Read the contents of all your json files and put them in a PHP array with json_decode function. Once they're in a format you're happy with export them with json_encode. Commented May 15, 2014 at 22:56

4 Answers 4

3

Based on your comment (echoing several json strings), you should do the following:

  1. Initialize an empty results array;
  2. Read your file and put it in an array or object using json_decode();
  3. Add this array / object to your results array;
  4. At the end, use json_encode() to encode and echo out your results array.
Sign up to request clarification or add additional context in comments.

Comments

1

You must make a JSON.parse(data) before attempting to access to data['a'], and send a header from PHP that implicitly say the browser that the data output is going to be a JSON.

header ('Content-Type: application/json');

2 Comments

jQuery will make an intelligent guess and if it recognizes the json, it will be parsed already by the time you get it.
It doesn't matter to the order of JSON.parse(data) on success callback, as it actually trigger an error which indicates it just parsed invalid json.
0

The problem might be that PHP is returning a string that looks like JSON.

In JS it might help to JSON.parse(data) to convert from string to JSON object, then you can access it.

$("#test_btn").click(function() {
  $.get("serverside.php", function(data, status) {
    $json = JSON.parse(data);
    console.log("data " , $json["a"]); // should now return 1
  });
});

Comments

0

you need to put json_encode or parse it in javascript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.