我正在尝试更新 ajax 收件箱。我在将 xml 解析为 jquery 时遇到问题。我总是得到错误,这不是一个 xml 文件。但它(认为?)是正确的,并且没有代码错误。
XML 输出:
<?xml version='1.0' ?>
<inboxdata>
<convid>2</convid>
<sender_id>1</sender_id>
<reciever_id>2</reciever_id>
<msg_count>4</msg_count>
<last_txt_msg>Hello</last_txt_msg>
<sender_read>1</sender_read>
<sender_delete>0</sender_delete>
<time>23.09.2013 - 19:38</time>
<userpic>img/ava/1.png</userpic>
</inboxdata>
<inboxdata>
<convid>4</convid>
<sender_id>1</sender_id>
<reciever_id>3</reciever_id>
<msg_count>0</msg_count>
<last_txt_msg>No msg</last_txt_msg>
<sender_read>1</sender_read>
<sender_delete>0</sender_delete>
<time>23.09.2013 - 20:25</time>
<userpic>img/ava/1.png</userpic>
</inboxdata>
PHP:
function show_inbox($mysqli)
{
if(!isset($_SESSION)){
sec_session_start();
}
$userid = $_SESSION['user_id'];
$get_conversation = $mysqli->query("SELECT * FROM conversation WHERE sender_id =" . $userid);
$xml_node = "";
while ($row = mysqli_fetch_array($get_conversation)) {
$xml_node .= "<inboxdata>\n";
$xml_node .= "<convid>".$row['convid']."</convid>\n";
$xml_node .= "<sender_id>".$row['sender_id']."</sender_id>\n";
$xml_node .= "<reciever_id>".$row['reciever_id']."</reciever_id>\n";
$xml_node .= "<msg_count>".$row['msg_count']."</msg_count>\n";
$xml_node .= "<last_txt_msg>".$row['last_txt_msg']."</last_txt_msg>\n";
$xml_node .= "<sender_read>".$row['sender_read']."</sender_read>\n";
$xml_node .= "<sender_delete>".$row['sender_delete']."</sender_delete>\n";
$xml_node .= "<time>".$row['time']."</time>\n";
$xml_node .= "<userpic>img/ava/1.png</userpic>\n";
$xml_node .= "</inboxdata>\n";
}
$returnXML = "<?xml version='1.0' ?>\n".$xml_node;
echo $returnXML;
}
爪哇:
function show_inbox()
{
result = $.ajax({
type: 'GET',
async: false, // WICHTIG!
url: 'functions_chat.php',
data: ({
a: "show_inbox"
}),
success: function(response){
alert($(response).find('inboxdata').length);
$(response).find('inboxdata').each(function(){
var convid = $(this).find('convid').text();
$("ul#chatinbox").prepend("<li>"+convid+"</li");
});
}
}).responseText;
}
如果 Alert fromalert($(response).find('inboxdata').length);
出现,它总是算 0。它应该算 2?
如果我使用$.parseXML(response)
我会得到 XML 错误:无效的 XML... 但为什么它无效?这对我来说似乎有效!
这里哪里有问题?谢谢!