GI89
GI89

Reputation: 1

Can't get value from JSON in AJAX call?

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",
      }
    ]
  ]
}

Upvotes: 0

Views: 587

Answers (3)

lacripta
lacripta

Reputation: 112

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",

         }
      ]
   ]
}

Upvotes: 0

JoeCodeFrog
JoeCodeFrog

Reputation: 418

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

Upvotes: 1

A. Blub
A. Blub

Reputation: 792

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 + '</>');
                    }
                }
            }

        }
    });
});

Upvotes: 0

Related Questions