jQuery AJAX请求返回XML

<suggestions>
<user>
<![CDATA[ Chris ]]>
</user>
<user>
<![CDATA[ Christina ]]>
</user>
</suggestions>

and the js part

            $.ajax({
                type: "POST",
                url: 'index.php/Suggest/',
                dataType: 'xml',
                data: {username: username},
                success: function(data) {
                    $(data).find('suggestions').each(function(){
                        if ($(this).find('user').text()) {
                            $('#container').html('<div>' + $(this).find('user').text() + '</div>');
                        }
                    });
                },
                error: function(request, status, error) {
                    // will also occur when the user toggles a window and instantly clicks on a link, so don't alert anything
                }
            });

inserts

<div>ChrisChristina</div>

but I want

<div>Chris</div> <div>Christina</div>

I think it iterates over the suggestions. Instead it should iterate over user. Here is the working code:

var response = ""+
"<suggestions>" +
"<user>" + 
"<![CDATA[ Chris ]]>" +
"</user>" + 
"<user>" + 
"<![CDATA[ Christina ]]>" + 
"</user>" + 
"</suggestions>";

// This is using the JSFiddle "echo" interface
$.post("/echo/xml/", {"xml": response}, function(data) {
    $(data).find('user').each(function(i, user){
        $("#container").append($("<div>").text($(user).text()));
    });
});​

You can view it live on JSFiddle

You should use append instead of html if you want to add all users:

    $(data).find('suggestions').each(function(){
        $(this).find('user').each(function(){
            $('#container').append('<div>' + $(this).text() + '</div>');
        });
    });

You will need to iterate over each user element. When using text() method on more than one element, all the text is returned but method has no way to identify to add spaces.

$(data).find('suggestions').each(function() {
    var $user=$(this).find('user');
      /* use length to check if user elements exist*/
     if($user.length){
          var user=$user.map(function(){
            return $(this).text();
          }).get().join(' ')

        $('#container').append('<div>' + user + '</div>');
    }
});