0
I have the following code:
$.ajax(
{
type: 'GET',
url : "chat/updates/",
async : true,
cache : false,
success: function(data){
if(data.success){
alert(data)
thread = data.messages;
senders = data.senders;
$.each(thread, function() {
if($("#chat-box").is(":visible")){
chatbuddy = $("#chat_buddy_id").val();
if(this.sender == chatbuddy){
li = '<li class="'+ this.type +'"><img src="assets/images/thumbs/'+this.avatar+'" class="avt img-responsive">\
<div class="message">\
<span class="chat-arrow"></span>\
<a href="javascript:void(0)" class="chat-name">'+this.name+'</a> \
<span class="chat-datetime">at '+this.time+'</span>\
<span class="chat-body">'+this.body+'</span></div></li>';
$('ul.chat-box-body').append(li);
$('ul.chat-box-body').animate({scrollTop: $('ul.chat-box-body').prop("scrollHeight")}, 500);
//Mark this message as read
$.ajax({ type: "POST", url: base + "chat/mark_read", data: {id: this.msg}});
}
else{
from = this.sender;
$.each(senders, function() {
if(this.user == from){
$(".chat-group").find('span[rel="'+from+'"]').text(this.count);
}
});
}
}
else{
from = this.sender;
$.each(senders, function() {
if(this.user == from){
$(".chat-group").find('span[rel="'+from+'"]').text(this.count);
}
});
}
});
var audio = new Audio('assets/notify/notify.mp3').play();
}
},
error : function(XMLHttpRequest, textstatus, error) {
console.log(error);
}
}
);
}, 2000);
the method GET
is returning me the following JSON:
{"success":true,"messages":[{"msg":"57","sender":"2","recipient":"1","mensagem":"oi","time":"Nov 13, 2015, 6:17 pm","tipo":"receive","nome":""},{"msg":"58","sender":"2","recipient":"1","mensagem":"oi","time":"Nov 13, 2015, 6:18 pm","tipo":"receive","nome":""}],"senders":[{"user":2,"count":2}]}
but there in the if(data.success)
he doesn’t pass the check! Someone can help me?
Have you tried debugging from the browser? Place a breakpoint there and run your page and see what is returned in the.sucess date.
– user28595
I already tried to give an Alert, and returns as Undefined !
– Wesley Igor
@Wesleyigor does not happen to give this Alert because the in ajax, the a comes from the asynchronous word. To check if the data is coming take a look at the networking tab of Chrome Developer tools, looking for the request made to /chat/updates
– Rafael Mena Barreto
I just tried to give Alert to test the date.Success ... but as you can see GET is returning me the value of Success, but does not pass the validation.. someone knows how to tell me why ?
– Wesley Igor
Do what I told you, run your website, open the firefox console by pressing control+shift+i and switch to "debug tab. Select your js and declare the
var retorno = data.sucess
just above theif
of the validation. Place a breakpoint next to the if(double click right next to the line) and run your page. When it stops, you will see what the.sucess date is returning. If it does not stop, it is because the request is in error.– user28595