2

In php:

header('content-type: application/json; charset=utf-8');
$res = array("success" => true, "status" =>array(),"path" =>array());
echo(json_encode($res));

I want to access path from above array.

In jQuery I am trying

on("success", function (Text) {
                console.log("l path " + Text[0].path);
            $.each(Text[0].path, function(i,k){
                       console.log(i+"  "+k);
});

It is throwing an error:

Uncaught TypeError: Cannot read property 'path' of undefined'

4
  • use response = JSON.parse(Text). Commented Nov 6, 2015 at 11:31
  • 1
    Use var obj = JSON.parse(Text); and use obj in each. OR use dataType: 'json', in $.ajax configuration options. Commented Nov 6, 2015 at 11:31
  • As mentioned by others, if you want to use the JSON you need to parse it first, otherwise it'll be a string. Commented Nov 6, 2015 at 11:32
  • echo(json_encode($res)); are you sure you are getting a valid json out of it? Commented Nov 6, 2015 at 11:36

2 Answers 2

3

Use JSON.parse(), like following:

on("success", function (Text) {
    var response = JSON.parse(Text);
    console.log("l path " + response.path);
    $.each(response.path, function(i,k){
        console.log(i+"  "+k);
    });
}
Sign up to request clarification or add additional context in comments.

6 Comments

Not that I down-voted it, but I can guess it happened because it was answered in the comment of the question.
I have also commented @Script47 but i can't put complete code in comment. So answer is best way to expose. I don't know why people downvote without reason. Anyway let OP try this solution.
@Manwal {"success":true,"status":[],"path":[]} this is the output of php. so response[0].path should be response.path
LOL, last time I checked this question, there was one down vote to question and +1 to answer, and this time, -1 to answer and +1 to question. I'm drunk, I suppose :P
@Manwal It is showing Object {success: true, status: Array[1], path: Array[1]}path: Array[1]0: Array[1]0: "example.com/images/a/post/ee.jpg"length: 1__proto__: Array[0]length: 1__proto__: Array[0]status: Array[1]0: Objectmessage: "ok"success: true__proto__: Objectlength: 1__proto__: Array[0]success: true__proto__: Object (index):29 [Array[1]]0: Array[1]0: "example.com/images/a/post/ee.jpg"length: 1__proto__: Array[0]length: 1__proto__: Array[0] localhost/:1 Uncaught SyntaxError: Unexpected token o
|
0

I'd use

on("success", function (Text) {
var response = JSON.parse(Text);
console.log("l path " + response[0].path);
$.each(response[0].path, function(i,k){
    console.log(i+"  "+k);
});
}

1 Comment

Is this PHP Code???? How can you use this code in PHP? This one looking exact replica of my answer and got upvoted. Very Nice.

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.