1

I'm new to jQuery. I'm trying to get some data from MySQL via AJAX. My PHP returns JSON. This is the response (don't mind variables names. It's in Czech :)):

  [{"nadpis":"Testovac\u00ed nadpis","text":"Testovac\u00ed \u010dl\u00e1nek o tom jak se zase prohr\u00e1lo","sestava":"Nikdo nehr\u00e1l"},{"nadpis":"Druhej nadpis","text":"Druhej text","sestava":"druh\u00e1 sestava"}]

Here is my jQuery function:

 $.ajax({ type: 'GET',   
                 url: 'db.php',   
                 datatype:'json',
                 success : function(data)
                 {  console.log(data[1].text);
                    console.log(data);
                 }
        });

The problem is that when I want to access data[1].text, it only returns undefined. I went through a lot of answers here on StackOverflow and other forums, but I still can't make it work.

andrle
  • 11
  • 1
  • 3

1 Answers1

2

Change datatype to dataType. Otherwise jQuery doesn't recognize the option and won't parse the response for you.

In your case, data is still a string, which you can verify with console.log(typeof data). data[1] returns "{", and "{".text is undefined.

Have a look at the documentation for the correct option names: http://api.jquery.com/jquery.ajax/


Alternatively, you could parse the response yourself.

(I posted an answer to prevent other misguiding answers. It's community wiki because I voted to close the question.)

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Thank you .). It works now. Such a stupid mistake. I've spent half a day trying to solve it and all I need is uppercase T :D – andrle Sep 24 '14 at 20:34