0

在下面的代码中,我尝试从 A url 中读取书籍详细信息,但它不显示输出。在输出屏幕中单击 ajax 按钮时,它不会进入$(data).find("Book").each(function(){});

我的代码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>

请告诉我为什么没有得到输出。

在 ios 模拟器中运行代码时,我只会收到此警报消息

如果我在 chrome 中运行该文件,我将获得此输出但不返回 Web 服务输出

4

1 回答 1

0

我认为success函数的data参数是从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...}
于 2012-09-28T14:01:28.017 回答