1

I have following ajax function.

$.ajax({
        url: "/ajax",
        type: "get",
        data: values ,
        success: function (data) {

           console.log(data); // This is Line 7
           console.log(data[0].first_name);  //// This is Line 8      

        },
        error: function(jqXHR, textStatus, errorThrown) {
           console.log(textStatus, errorThrown);
        }


    });

Line 07 Output : [{"id":2,"first_name":"Nimalka","last_name":"Perera","address":"Kandy","age":25,"created_at":"2017-08-29 07:23:43","updated_at":"2017-08-29 07:23:43"}]

Line 08 Output : undefined

Why is line 08 undefined?

When I check the output with JSFiddle it works Fine. ( Link to the JsFiddle)

But why it is undefined in inside $.ajax function?

3
  • 3
    You are getting a string response. First convert it to object using JSON.parse. Commented Aug 29, 2017 at 11:43
  • @Shubham Thanks. It worked :-) Even I return as Json in PHP File (return json_encode($students); ) why it getting as string? Commented Aug 29, 2017 at 11:46
  • javascript is not expecting an Object. You have to set dataType to JSON. Commented Aug 29, 2017 at 11:49

3 Answers 3

3

You need to convert the data into JSON then access the key value

 success: function (data) {
           console.log(data); 
           var response = JSON.parse(data);
           console.log(response[0].first_name);        

        }
Sign up to request clarification or add additional context in comments.

Comments

1

As your question is why I will explain what happens.

As other have mentioned, typeof data == "string". Ths means that data[0] will be the first character in your string.

In case of [{"id":2,"first_name":"Nimalka","last_name":"Perera","address":"Kandy","age":25,"created_at":"2017-08-29 07:23:43","updated_at":"2017-08-29 07:23:43"}] it will be character '['

As such, data[0].first_name means that JavaScript will get the value for key first_name of Object '[', which is not defined, thus returning undefined.

Of course, the solution is to parse the string to json, as other mentioned.

2 Comments

I do not think this explanation is relevant here.
Actually I believe it helps others learn how to debug themselves, so it's very relevant
1

A better and simple thing to do here is, on your Ajax configuration you can set the expected Response type is of JSON. This will force Jquery to parse the data for you.

Here is the code:

$.ajax({
        url: "/ajax",
        type: "get",
        data: values ,
        dataType:"JSON",
        success: function (data) {

           console.log(data); // This is Line 7
           console.log(data[0].first_name);  //// This is Line 8      

        },
        error: function(jqXHR, textStatus, errorThrown) {
           console.log(textStatus, errorThrown);
        }


    });

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.