The first one - using load() to filter the response - unfortunately
load() will then discard the remainder of the XML response. I want to
pass the HTML fragment as part of a larger XML message and use the
data from the other fields as well. I would have to make the request
twice (once for the HTML fragment, once for the XML fields and things
will have changed server-side in the interim). I am using $.ajax(). I
haven't testing load() but I suspect in any case it may require a text
(rather than XML response) from the server to work, much like $.ajax
().
In your second example, yes this works because you are passing HTML to
the after() method (provided of course you use valid HTML element
tags, such as span or div rather than "frag"). However the problem I
have is passing XML to after() rather than HTML....
To demonstrate, I quickly wrote up the example above into code and
tested it. This is the XML file ("response.xml") sitting on the
server:
<?xml version="1.0"?>
<response>
<cityid>2</cityid>
<frag>
<div class="citybox">
<h2 class="citytitle">City of London</h2>
<p class="desc">London is on the River Thames</p>
</div>
</frag>
</response>
And this is the requesting webpage:
<html>
<head>
<script type='text/javascript' src="jquery.js"></script>
<script type='text/javascript'>
$(function(){
$("#button").click(function(){
$.ajax ({
url: "response.xml",
success: function(data, status) {
alert ("Received City ID "+$(data).find("cityid").text
());
$("#title").after($(data).find("frag").children());
}
});
});
});
</script>
</head>
<body>
<p id=title>Cities of the World</p>
<input type=button id=button value=Get>
</body>
The above receives the XML message, displays the city ID correctly,
but then fails to insert the HTML fragment into the webpage...
No comments:
Post a Comment