2012-09-17 59 views
0

朋友我有一个问题。我有一个会员区域的功能,它会显示使用jQuery的收件箱邮件的计数。

这就是我所做的。

function listner_requests(){ 
    $.ajax({ 
    type: "POST", 
    url: "http://src.abc.com/listner.php", 
    data: "action=listner_requests", 
    success: function(responseText, statusText, xhr) { alert(responseText);        
    }, 
error: function(data, statusCode) { 
    alert("ERROR: "+data) 
    } 
    }); 
    } 

listner.php

if($_POST['action']=="listner_requests"){ 
$sql="SELECT messages FROM friend_requests WHERE message_to='".$_SESSION['MEMBER_ID']."'"; 
$data=mysql_query($sql,$profile); 
$count=mysql_num_rows($data); 
echo $count; 
exit(); 
} 

我excecute的PHP没有Ajax来测试它是否工作或没有。它是完美的工作。

但如果我使用ajax方法,它不会提示正确的响应。 它会产生一个错误。我做错了什么吗?请帮帮我。感谢

+1

你什么错误? –

+0

检查您的浏览器控制台是否有任何错误。 –

+0

错误:[对象对象]这是我得到的警报 – vinu

回答

1

不是一个解决方案 - 但一个评论,以帮助调试(不能在评论后的代码不会出现)

您的代码:

success: function(responseText, statusText, xhr) { 
     alert(responseText);         
    }, 
    error: function(data, statusCode) { 
     alert("ERROR: "+data) 
    } 

是有点误导,因为你有参数混乱起来。你会发现它更清晰与调试:

success: function(data) { 
     alert(data);         
    }, 
    error: function(xhr, status, textError) { 
    alert('Error ' + xhr.responseText + ' - ' + status + ' - ' + textError); 
    } 

如果xhr.responseText是空白的(如你在评论中说的),然后textError可能会揭晓答案。如果做不到这一点,状态会...

+0

感谢您的评论@Robbie它只是显示“错误 - 错误 - ”,这意味着状态是错误的,其他两个是空白的 – vinu

0

假设ü要算作回应。我宁愿返回的数据作为JSON ... 检查了这一点...看看它是否工作......

function listner_requests(){ 
$.ajax({ 
type: "POST", 
dataType:'json', 
url: "http://src.abc.com/listner.php", 
data: "action=listner_requests", 
success: function(data) { alert(data.totalcount);        
}, 
error: function(data, statusCode) { 
alert("ERROR: "+data) 
} 
}); 
} 

listener.php

if($_POST['action']=="listner_requests"){ 
$sql="SELECT messages FROM friend_requests WHERE   message_to='".$_SESSION['MEMBER_ID']."'"; 
$data=mysql_query($sql,$profile); 
$count=mysql_num_rows($data); 
echo json_encode(array('totalcount'=>$count)); 
exit(); 

}