Which code segment should you use?

A Web service returns a list of system users in the following format.
<?xml version=”1.0″ ?>
<users>
<user id=”first”>
<name>Name of first user</name>
<email>[email protected]</email>
</user>
<user id=”second”>
<name>Name of second user</name>
<email>[email protected]</email>
</user>
</users>
You need to populate a drop-down menu with the IDs and names of the users from the Web service,
in the order provided by the service. Which code segment should you use?

A Web service returns a list of system users in the following format.
<?xml version=”1.0″ ?>
<users>
<user id=”first”>
<name>Name of first user</name>
<email>[email protected]</email>
</user>
<user id=”second”>
<name>Name of second user</name>
<email>[email protected]</email>
</user>
</users>
You need to populate a drop-down menu with the IDs and names of the users from the Web service,
in the order provided by the service. Which code segment should you use?

A.
$.ajax({
type: “GET”,
url: serviceURL,
success: function(xml) {
$.each($(xml), function(i, item) {
$(“<option>”).attr(“value”, id)
.text(tx).appendTo(“#dropdown”);
});
}
});

B.
$.ajax({
type: “GET”,
url: serviceURL,
success: function(xml) {
$(xml).find(“user”).each(function() {
var id = $(this).id;
var tx = $(this).name.text;
$(“<option>”).attr(“value”, id)
.text(tx).appendTo(“#dropdown”);
});
}
});

C.
$.ajax({
type: “GET”,
url: serviceURL,
success: function(xml) {
$(xml).find(“user”).each(function() {
var id = $(this).attr(“id”);
var tx = $(this).find(“name”).text();
$(“<option>”).attr(“value”, id)
.text(tx).appendTo(“#dropdown”);
});
}
});

D.
$.ajax({
type: “GET”,
url: serviceURL,
success: function(xml) {
xml.find(“user”).each(function(node) {
var id = $(node).attr(“id”);
var tx = $(node).find(“name”).text();
$(“<option>”).attr(“value”, id)
.text(tx).appendTo(“#dropdown”);
});
}
});

Explanation:
each() function
(http://api.jquery.com/each/)
CHAPTER 9 Working with Client-Side Scripting, AJAX, and jQuery
Lesson 3: Implementing jQuery
Implementing AJAX with jQuery



Leave a Reply 0

Your email address will not be published. Required fields are marked *