0

I am using a file upload plugin for one of my sites and a callback function of the "Delete" option returns a jSON response that looks like this:

{"5425ba85c976a~2014-09-26_7-13-47.png":true}

How would I go about parsing this jSON response in jQuery to know if the value is true or false?

This is what my code looks like but it prints undefined when i log this

$(document).on('click', '.delete', function(e) {

    var fileName = $(this).attr('filename');
    $.ajax({
        url: "server/php/?action=DELETE&file="+fileName,
        type: 'GET',
        error: function (err) {
            alert(err.statusText);
        },
        success: function (data) {

            console.log(data.fileName);

        }
    });
});
SBB
  • 8,560
  • 30
  • 108
  • 223
  • can you show us how the html element looks like? would recommend the data attribute for this btw. – malifa Sep 26 '14 at 19:25
  • You need `object.keys()`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys. – loveNoHate Sep 26 '14 at 19:26
  • To compare your case to the duplicate: just like the example wrongly does `something.foo` but really wants the property named "`bar`" (which is stored in `foo`), you're wrongly doing `data.fileName`, when you really want the property named "`542...47.png`", which is stored in `fileName`. Bracket notation is your friend here: where the example solution is `something[foo]`, yours is `data[fileName]`. – apsillers Sep 26 '14 at 19:38
  • @apsillers shouldnt `console.log(data[fileName]);` work then? Returns undefined – SBB Sep 26 '14 at 19:40
  • @SBB Yes, it should. I assumed the value of `fileName` was the same as the property name in the response; is it not? Also, just to make sure, are you checking the value inside of the `success` function? – apsillers Sep 26 '14 at 19:42
  • It is in fact, fileName like the example. That line is in the success callback of the ajax statement – SBB Sep 26 '14 at 19:42
  • What do you see when you `console.log(Object.keys(data), fileName)`? Do the first element of the `Object.keys` array and the `fileName` value match? – apsillers Sep 26 '14 at 19:44
  • you might need to trim the value of the attribute from the element: `var filename = $.trim($(this).attr('filename'));`. I would also run a console.log("is equal?", (filename == '5425ba85c976a~2014-09-26_7-13-47.png')), right after you define filename. It is possible that there is some difference between what you think you have and what is really defined. – scrappedcola Sep 26 '14 at 19:47

1 Answers1

1

you have to do

data["5425ba85c976a~2014-09-26_7-13-47.png"]

instead of

console.log(data.fileName);
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
  • that would be the way to get an array value, not a json property. – malifa Sep 26 '14 at 19:26
  • 1
    @Razah [`works for both`](http://jsfiddle.net/8t3grt65/) :) – Mithun Satheesh Sep 26 '14 at 19:27
  • @Razah if you are trying to use the value of a variable you have to use []. If you use `data.fileName` it will look for a property on the object named "fileName". using the bracket notation will look for a value on the object of the value of the variable `fileName`. – scrappedcola Sep 26 '14 at 19:30
  • yes you're absolutely right and i should think more before i comment ;) – malifa Sep 26 '14 at 19:31
  • Since the image name is dynamic, I did `console.log(data[fileName]);` but it returned undefined – SBB Sep 26 '14 at 19:41
  • if `data["5425ba85c976a~2014-09-26_7-13-47.png"]` works and `data[fileName]` doesnt. its high time u console log fileName and check whats in it – Mithun Satheesh Sep 26 '14 at 19:42
  • console.log(fileName) returns the name of the file where as console.log(data[fileName]) returns undefined – SBB Sep 26 '14 at 19:44
  • did `data["5425ba85c976a~2014-09-26_7-13-47.png"]` work when u tried? – Mithun Satheesh Sep 26 '14 at 19:47
  • see [`this fiddle`](http://jsfiddle.net/ea0u2gyu/). it has the use case you have and works. Or i missed something? – Mithun Satheesh Sep 26 '14 at 19:49