-1

I want to fetch the values in the List array from the json object.

Here is my JS / Ajax code:

 $.ajax({
   type: 'GET',
   url: "http://127.0.0.1:8000/api/sort_api/" + a,
   contentType: 'application/json',
   dataType: 'json', //specify jsonp
   success: function(data) {
     var htmlData= '';
     for(var i=0; i<data.length; i++){
       htmlData+= '<li>'+data[i]+' </li>';
     }
     $('#list').html(htmlData);
     // alert(list);
     console.log(data);
   }.bind(this),
     error: function(e) {
       console.log('error', e);
     }
   }); 

Here is the console log(data) result:

 {status: "200", status_message: "List Sorted", List: Array(5)}
 List:(5) ["Abc", "Take 78A", "Take Airport", "Take flight", "Take flight"]
 status:"200"
 status_message:"List Sorted"

When i write console.log(data['status']) It shows the value 200 in console

But when i write console.log(data['list']); it shows undefined

Can somebody tell me what i am missing? I want to retrieve the array(List) in that object

3
  • 1
    console.log(data.List); try this Commented Apr 23, 2018 at 8:18
  • 1
    Did you try console.log(data['List'])? Its case sensitive Commented Apr 23, 2018 at 8:18
  • Yo have to do this: data = JSON.parse(JSON.stringify(data[0]));. And you have the same issue in this LINK Commented Apr 23, 2018 at 8:19

2 Answers 2

1

Try with each

$.each(data, function( index, value ) {
  htmlData+= '<li>'+value+' </li>';
});
Sign up to request clarification or add additional context in comments.

Comments

-1

try in capital List not list

console.log(data.List)

or

console.log(data['List'])

both will work.

2 Comments

do you think, is it required to answer this question. @zeropublix and me already commented this
The question should be closed because it's a basically the result of a typo.

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.