2

I have a $.get() statement, which returns this (result from a console.log()):

{"desc":"asdasda","dateD":"2012-08-31","dateE":"2012-09- 01","image":"fasdasdasd","categorie":"3"}

Now when I try, in Javascript, to manipulate the array, everything holds an undefined or null value:

var image = data.image;
desc = data.desc;
dateD = data.dateD;
dateF = data.dateE;
image = data.image;
categorie = data.categorie;

Note: the DateF= data.dateE is not a mistake.

Note2: Those statements are all held within the function (data){} function contained in the $.get().

All those assignments return undefined. What am I doing wrong? I have read and re-read the official jQuery doc, without success.

9
  • 1
    Get is cool, but it doesn't expect the object to be returned in JSON. console.log() natively detects the type of array, object, or string passed, and it parses it accordingly. Which it why it looks good. If you want to getJSON, then use $.getJSON, otherwise, use $.ajax() and set the dataType: json. Commented Aug 6, 2012 at 15:20
  • Have you set the dataType to JSON? Your console.log() show a Javascript object literal expressed as a string - is it still just a string? Commented Aug 6, 2012 at 15:20
  • Are you using $.parseJSON(data) ? Commented Aug 6, 2012 at 15:21
  • @Ohgodwhy I suggest you post this as an answer so I can accept it. Thanks a lot sir. Commented Aug 6, 2012 at 15:23
  • 2
    @Ghillied Neal's got the jist of it. Accept his! Commented Aug 6, 2012 at 15:24

1 Answer 1

3

Make sure you set the dataType of the return to json.

If you don't do this, the result data may be a string and you will need to use JSON.parse(data) to turn it into a usable object.


For example:

$.get(url, getData, function(data){
    //your fn...
}, 'json');
Sign up to request clarification or add additional context in comments.

Comments

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.