1
function addFav(id){
    $.ajax({
      url: "misc/favAdd.php",
      type: "POST",
      data: { mode: 'ajax', user: id},
        dataType: 'json',
      success: function(data, status){
                    if(typeof(data.error) != 'undefined') {
                        if(data.error != '')
                            alert(data.error);
                    } else {
           $('a#fav')
                 .addClass('active')
                 .attr('title','[-] Remove as favorite')
                 .unbind('click')
                             .bind('click', function() { removeFav(id); })
           ;
                jGrowlTheme('wallPop', 'mono', '[+] Favorit', 'Du har nu lagt till denna profil som favorit', 'images/addFavorit_hover2.png', 1000);
      }
      }
    });
}

I have this, and i wish to alert the data.error, if it gets any error response, and that works. But now, if i dont have any data.error it doesnt run the addClass and stuff thats after }else{ ?

What have i done wrong?

Karem
  • 17,615
  • 72
  • 178
  • 278
  • Looks alright to me. Are you sure that is the correct response? It is not in an Array? – user113716 Sep 05 '10 at 16:24
  • Its not in a array. please check updated question with the PHP, maybe if theres something wrong there – Karem Sep 05 '10 at 16:58
  • @Karem - We've answered you on this issue 2 or 3 times now...your JSON **must be valid**, please, *please* learn from your questions. – Nick Craver Sep 05 '10 at 17:09
  • It is valid?? I am learning from my questions else i wouldnt have gotten this far.. thank you – Karem Sep 05 '10 at 17:12
  • @Karem - It's not valid, and we've alerted you to this issue, most recently here: http://stackoverflow.com/questions/3640012/jquery-running-success-in-1-3-2-but-not-in-1-4-2/3641933#3641933 This was yesterday... – Nick Craver Sep 05 '10 at 17:13
  • yes that was because it was wrapped in () , this one is wrapped in { } like it should be? – Karem Sep 05 '10 at 17:43
  • @Karem - I provided you a link to check if it's valid, was it really harder to paste the output there and see *exactly* what's invalid (quotes) than to type up a new question? You have to learn to help yourself, *plenty* of people here have given you the tools to do so, don't ignore them. – Nick Craver Sep 05 '10 at 17:50
  • @Nick Craver , i understand how you feel and i feel you 100%. You're right i already forgot about that link, but its bookmarked for good now. Thank you. I found my fault i was missing ""´s.. – Karem Sep 05 '10 at 20:52
  • But now, if it doesnt have any error´s, then its not running the addClass and so..? Question updated – Karem Sep 05 '10 at 20:54

1 Answers1

0

What if you used this instead?

function addFav(id) {
    $.ajax({
        url: "misc/favAdd.php",
        type: "POST",
        data: {
            mode: 'ajax',
            user: id
        },
        dataType: 'json',
        success: function(data, status) {
            if (data.error !== undefined && data.error != '') {
                alert(data.error);
            } else {
                $('a#fav')
                .addClass('active')
                .attr('title', '[-] Remove as favorite')
                .unbind('click')
                .bind('click',
                function() {
                    removeFav(id);
                });
                jGrowlTheme('wallPop', 'mono', '[+] Favorit', 'Du har nu lagt till denna profil som favorit', 'images/addFavorit_hover2.png', 1000);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}

I suspect what is happening is your having a failure getting any (valid) data, and since you haven't defined an error handler, the response is just getting dropped on the floor. Your test for error relies on their being data to begin with and so will only trigger if you get back valid data that contains an 'error' property in the object.

mkoistinen
  • 7,724
  • 3
  • 41
  • 56