0

im a newbie :)

I searched my questions, there are some topic but they dont are like my specific question.

I have an Ajax call for update a form, i can see the values in the console.log(), but I can't take the values for insert in the input or divs, here my code:

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: 'my-web-service',
        dataType: "text",
        success: function(data) {
            console.log(data)
            var json = $.parseJSON(data);

            for (var i = 0; i < json.length; ++i) {
                $('#json').append('<div>' + json[i].name + '</div>');
            }
            console.log(json)
        }
    });
});

My JSON data is :

{
  "experience": [
    [
      "58b407cd30f8c7a508004210",
      {
        "artistInfo": {
          "id": "f8d3a411",
        }
      },
      {
        "name": "test",
        "description": "testing",
        "tipology": null,
        "email": "ext_link",
        "externalLink": "www.mywebstite.com",
      }
    ]
  ]
}
13
  • 1
    so what does the data look like? Commented Feb 27, 2017 at 14:16
  • 2
    Is it not '<div>' + json[i].name + '</div>' ? Commented Feb 27, 2017 at 14:17
  • '<div>' + json[i].name + '</div>' Commented Feb 27, 2017 at 14:17
  • You'll need to include at least the structure of the response even if you want to change the specific data returned. Commented Feb 27, 2017 at 14:19
  • dataType: "text" is that correct? Commented Feb 27, 2017 at 14:21

3 Answers 3

1

Experience is an array within an array(???), You should fix it or access it accordingly.

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

Comments

0

i prefere to remove your dataType condition, change the type to POST (you need it when your would send some Data) and skip your JSON parser, it is already parsed and would throw an Exception.

$(document).ready(function() {
    $.ajax({
        type: "POST",
        data: { }, // when you need it
        url: 'my-web-service',
        success: function(json) {
            console.log(json)

            for (var i = 0; i < json['experience'].length; ++i) {
                for (var i2 = 0; i2 < json['experience'][i].length; ++i2) {
                    if( json['experience'][i][i2].name ) {
                        $('#json').append('<div>' + json['experience'][i][i2].name + '</>');
                    }
                }
            }

        }
    });
});

4 Comments

Thank your for the answer, with type POST i get Undefined values, with GET i get nothing....but in console.log(json) i can see the data...so i don't know how to fix
When I use type:POST i get nothing, with GET I get a single Undefined Item
EDIT: with json['experience'][i][2].name i get the data, but i can't see the other values, like [i][3], [i][4]
here you have now all names of all array elements
0

for the look of it, if you can see the json in the console log, then is probably a problem of data type, are you sure it is an array?

json[i].name

this could be an object and not accesible as an array. check for the dataType. or post the output of your WS

after reading your post of the data is very clear you are trying to access it the wrong way, it as an array were your first entry is a String, then comes an Object which holds no property name, and finally a third Object which i guess you are trying to get the name.

json['experience'][2].name

{  
   "experience":[  
      [  
         "58b407cd30f8c7a508004210",
         {  
            "artistInfo":{  
               "id":"f8d3a411",

            }
         },
         {  
            "name":"test",
            "description":"testing",
            "tipology":null,
            "email":"ext_link",
            "externalLink":"www.mywebstite.com",

         }
      ]
   ]
}

1 Comment

How I can access to Third Object without using an array? Because I tried to use "json[2].name" and I got undefined values

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.