jquery: json data.error else
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?
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.
精彩评论