clearly i must have overlooked something. here is my script, and below that is the data.
$("#kenteken").blur(function(){
var sRegistrationNr = $(this).val();
var sChassisNumber = false;
$.ajax({
type: "GET",
url: "/activeContent/warrantyClaim/ajax-xml-response.php",
data: "return=auto&kenteken="+sRegistrationNr,
dataType: "xml",
success: function(xml) {开发者_StackOverflow中文版
$(xml).find("xmlresponse").each(function(){
$(this).find("data").each(function(){
var sChassisNumber = $(this).find("chassisnummer").text();
});
});
}
});
alert(sChassisNumber);
});
here is the data from the xml file (responding fine)
- <xmlresponse>
<result>GPZB89</result>
- <data>
<kenteken>GPZB89</kenteken>
- <chassisnummer>
- <![CDATA[ KNEFA2253N5000176
]]>
</chassisnummer>
</data>
</xmlresponse>
Where does this go wrong?
$.ajax({
...
success: function(xml) {
var sChassisNumber= $(this).find("chassisnummer").text();
}
});
alert(sChassisNumber);
You are reading the results of the callback function before the AJAX request completes and calls the function back.
The ‘A’ in AJAX stands for asynchronous. The operation is still ongoing when the script gets to the line following the $.ajax()
call. That's why you have to pass a callback function in to execute when it's finished.
Install firebug and see what is being returned in detail:
console.log(xml);
console.log($(xml)); //this will be clickable in console.
click and explore the object. maybe the $(xml) is already the xmlresponse node and You try to find another in it
精彩评论