1

In my Javascript and PHP, I've manage to do an .ajax call to get an array. However when I want to display the display the values of each object I'm unable to do so.

PHP:

$request =  '{"request_id":' .$requestId. ', "reqName":"' .$requestName. '", "reqSubject":' .json_encode($requestSubjects). '}';

array_push($requestArray, $request);

echo json_encode($requestArray);

So when I console.log(data) it looks like this:

["{"request_id":13, "reqName":"Rin", "reqSubject":["English","A Math"]}", "{"request_id":14, "reqName":"Rin", "reqSubject":["English","E Math"]}"]

When I console.log(data[0]), I'm able to get the first object:

{"request_id":13, "reqName":"Rin", "reqSubject":["English","A Math"]}

However when I want to get the values of the object like so, console.log(data[0].request_id), it returns an undefined.

Would highly appreciate if someone could tell me what's wrong with my codes. Thank you.


2
  • Your response is JSON it means String. You should parse it with: var result=JSON.parse(data[0]); Then result.request_id will give you what you want.. Commented Feb 4, 2015 at 12:11
  • it should be work data[0]["request_id"] Commented Feb 4, 2015 at 12:11

4 Answers 4

2

["{"request_id":13,
note the " directly after the [. That's not the output format of an array containing objects in the javascript console, but an array that contains strings.
And that's because of

$request =  '{"request_id":' .$requestId. ', "reqName":"' .$requestName. '", "reqSubject":' .json_encode($requestSubjects). '}';

in your php script. You're not building a hashmap/object here but a string. And it is encoded as such by json_encode.

Try it with

$request = array(
    "request_id" => $requestId,
    "reqName" => $requestName,
    "reqSubject" => $requestSubjects,
)
array_push($requestArray, $request);

echo json_encode($requestArray);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use JSON.stringify() to convert the objects to string.

Just try it out

$request =  {"request_id":"aaaa", "reqName":"bbbb", "reqSubject":"ccc"};
 alert(JSON.stringify($request.request_id));

Comments

0

The response you are getting is in json format. You have to parse it first. Try with -

var response = jQuery.parseJSON(data[0]); //parse json data
console.log(response.request_id);

1 Comment

I think this might be right, but an answer should explain why it is an answer and not just dump code at the reader.
0

In your post you mentioned that

"However when I want to get the values of the object like so,
console.log(data[0].request_id), it returns an undefined."

To treat the result as an object it should be parsed to JSON object by using JSON.parse() method
Otherwise you can use it as an array

data[0]["request_id"]

2 Comments

data[0]["request_id"] and data[0].request_id are equivalent in JS.
can you please give a proper solution to the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.