2012-09-28 75 views
0

在下面的代碼我試圖讀取書細節格式的URL,但它不顯示output.In輸出畫面的同時單擊按鈕,AJAX,它不會進入到$(data).find("Book").each(function(){});webservice的jQuery函數

我的代碼html:

<html> 
<head> 
<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.js" type="text/javascript" charset="utf-8"></script> 
<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
function bodyload() { 
    alert("We are calling jquery's ajax function and on success callback xml parsing are done"); 
    $.ajax({ 
     url: 'http://www.edumobile.org/blog/uploads/XML-parsing-data/Data.xml', 
     dataType: 'application/xml', 
     timeout: 10000, 
     type: 'GET', 
     success: function(data) { 
      alert("inside success function"); 
      $("#bookInFo").html(""); 
      $("#bookInFo").append("<hr>"); * * // my code work until here after that i will not receive any book details in my web page.** 
      $(data).find("Book").each(function() { 
       $("#bookInFo").append("<br> Name: " + $(this).find("name").text()); 
       $("#bookInFo").append("<br> Address: " + $(this).find("address").text()); 
       $("#bookInFo").append("<br> Country: " + $(this).find("country").text()); 
       $("#bookInFo").append("<br><hr>"); 
      }); 
     }, 
     error: function(XMLHttpRequest, textStatus, errorThrown) { 
      alert("Error status :" + textStatus); 
      alert("Error type :" + errorThrown); 
      alert("Error message :" + XMLHttpRequest.responseXML); 
      $("#bookInFo").append(XMLHttpRequest.responseXML); 
     } 
    }); 
}​ 
</script> 
</head> 
<body> 
<center><button onclick="bodyload()">Ajax call</button></center> 
<p id="bookInFo"></p> 
</body> 
</html> 

請告訴我爲什麼沒有得到輸出。

while running the the code in ios simulator i ll gett this alert msg only

if i run the file in chrome i will get this output but not returns the web service output

+0

你有沒有調試? 'console.log(data);'顯示什麼? – epascarello

+1

你應該刪除其中一個對'jquery.js'的引用,你只需要一個。 –

回答

0

我認爲成功函數的數據參數是XML解析的JS對象,而不是HTML \ DOM。你不能對它應用$()。儘量

for(var i = 0; i < data.length; i++) {var name = data[i].name; etc...} 

for(var prop in data){var name = data[prop].name; etc...}