0

I have this script:

<script type="text/javascript">
        function requestimg(username){
                $.ajax({
                    url: '{% url "image" %}',
                    data: {
                      'username': username
                    },
                    dataType: 'json',
                    success: function (data) {
                        if (data) {
                            //Printing the whole data
                            console.log(data);
                            //Printing to see where I am
                            console.log("Name: ");
                            //Trying to rint the name
                            console.log(data[0].nombre);
                        }else {
                             alert("May Day");
                        }
                    }
                });
        }

I have a problem reading the properties in the json object When i print the data I get this:

{
    json: "[
        {
            "model": "polls.imagen",
            "pk": 17,
            "fields": {
                "n…"36",
                "imagen": "polls/static/pictures/dr.jpg"
            }
        }
    ]"
}

And when i print the data as I have it on the code i get:

Uncaught TypeError: Cannot read property 'nombre' of undefined

I have tryied writing it like data.nombre but i just get undefined

I have also tried this console.log(data[0].fields); , data[0].model, data.model

IMPORTANT I want to clarify that i wont know why there are 2 json objects im supposed to get just one and even if i try to put a [1] instead of the [0] i get the same mistakes.

I've tried answers from some previous similar questions and they didn't help.

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
Jose Ramon
  • 65
  • 1
  • 10
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json). – Teemu Aug 15 '17 at 07:14
  • technically you should be able to access the `nombre` field with `data[1].fields.nombre` (it seems to be the second item in the array, so `data[1]` not `data[0]`) – donnikitos Aug 15 '17 at 07:15
  • @donnikitos I have tried that and it says Uncaught TypeError: Cannot read property 'fields' of undefined – Jose Ramon Aug 15 '17 at 07:17
  • @JoseRamon I just unfolded your response, try `data[0].json[0].fields.nombre` that should do it – donnikitos Aug 15 '17 at 07:19
  • @donnikitos Uncaught TypeError: Cannot read property 'json' of undefined, I've tried it with `data[0]`, `data[1]`, `data[n].json[n]` – Jose Ramon Aug 15 '17 at 07:22
  • @JoseRamon what is `data.json` giving you? your console.log is not very clear. Is it one response or two? – donnikitos Aug 15 '17 at 07:23
  • @donnikitos `[{"model": "polls.imagen", "pk": 17, "fields": {"nombre": "55", "pub_date": "2017-08-08T05:52:38Z", "tecninca": "yyyy", "tamano": "yyyyyyyy", "precio": "yyyyy", "size": "36", "imagen": "polls/static/pictures/dr.jpg"}}]`, if i try `data.json.fields` it gives me undefined – Jose Ramon Aug 15 '17 at 07:27
  • @JoseRamon can you try this `data.json[0].fields.nombre` and see what value it returns – RohitB97 Aug 15 '17 at 07:33

2 Answers2

2

You will be able to access the fields per

data.json[0].fields

the response of your url is an array, named json

Additionally data.json is returning a string, so convert it to JSON with

data.json = JSON.parse(data.json);
donnikitos
  • 776
  • 1
  • 9
  • 18
0

Convert it to JSON first JSON.parse(data.json)

This will give you the access to the fields object data.json[0].fields

RohitB97
  • 440
  • 2
  • 6
  • 16