1

I'm using AJAX call and I'm generating this JSON object which is sent back to JavaScript. When I receive it in the JavaScript I'm not able to get values from it.

PHP:

echo json_encode(array("results" => array(array("user" => $member['user']),array("company" => $member['company']))));

JS:

success: function(response) 
    {
        for(var i = 0;i < response.results.length; i++)
        {   
          sessionStorage.setItem('user',response.results[i].user);
          sessionStorage.setItem('company',response.results[i].company);
        }
    }

I'm not able to read any values from the response that I receive.

Response that I get is in this format:

{"results":[{"user":"David"},{"company":"something"}]}

What would be the proper way of reading this JSON object?

4
  • 3
    You need the json to format like this {"results":[{"user":"David", "company":"something"}]} Commented Aug 15, 2013 at 19:47
  • Are you getting errors or is the for loop never executing or what is the problem? Commented Aug 15, 2013 at 19:48
  • @Blazemonger According to his code, he may have multiple items like {"results":[{"user":"David", "company":"something"}, {"user":"D", "company":"S"}]} Commented Aug 15, 2013 at 19:50
  • 1
    You might have to parse the JSON first, unless you set the correct content type response headers or have the dataType: 'json' option in the Ajax call. Commented Aug 15, 2013 at 20:02

2 Answers 2

2

Since you are trying to access results[0].company and results[1].user which are undefined, and since only results[1].company and results[0].user are defined in your object, if you change your PHP as follows your JavaScript should work unless response is just a string:

echo json_encode( array(
    "results" => array(
        array(
            "user" => $member['user'],
            "company" => $member['company']
        )
     )
));

If response is just a string change your JavaScript code as follows:

success: function(response) 
{
    response = JSON.parse(response);

Or

dataType: "json",
success: function(response) 
{
Sign up to request clarification or add additional context in comments.

4 Comments

how do I access it in JS
I'm getting: TypeError: Cannot read property 'length' of undefined
@user123_456: Add dataType: 'json' to your Ajax options. Otherwise response is a string containing JSON, not an object.
That's strange. Could you post the contents of response or typeof response? If it's a string you can also do response = JSON.parse(response); or what Felix said.
0

In your JSON response your result contains an array with 2 objects. The first containing the user and the second containing the company. However, the your for loop you expect both elements in the array to have both user and company. It looks like you are trying to just send one user and their company. If that is the case then your code should be as follows:

PHP:

echo json_encode(array("results" => array("user" => $member['user'],"company" => $member['company'])));

JS:

success: function(response) 
    {
        sessionStorage.setItem('user',response.results.user);
        sessionStorage.setItem('company',response.results.company);
    }

Your response will look like this.

{"results":{"user":"David","company":"something"}}

1 Comment

Sorry I accidentally left part of the old code on the PHP side. With the new edits it should now work.

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.