开发者

jquery read xml not function

开发者 https://www.devze.com 2022-12-18 21:50 出处:网络
clearly i must have overlooked something. here is my script, and below that is the data. $(\"#kenteken\").blur(function(){

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号