Main.php
<!DOCTYPE html>
<html>
<head>
<title>Lesson 21: Easy AJAX Calls with jQuery</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js"></script>
</head>
<body>
<h1>Lesson 21: Easy AJAX Calls with jQuery load()</h1>
<p><a href="#">Click here to fetch HTML content</a></p>
<div id="result">
</div>
<script type="text/javascript">
$(document).ready(function()
{
var url = "lesson18_test.xml";
function processData(data)
{
var resultStr = "";
var items = $(data).find('language');
$(items).each(function(i)
{
resultStr += $(this).text() + '<br />';
$('#result').html(resultStr);
});
}
$('a').click(function()
{
$.get(url, processData);
});
});
</script>
</body>
</html>
lesson18_test.xml
<?xml version="1.0" ?>
<languages>
<language>PHP</language>
<language>Ruby On Rails</language>
<language>C#</language>
<language>JavaScript</language>
</languages>
以上代码来自一个教程:http://www.html.net/tutorials/javascript/lesson21.php
问题:
有没有办法可以看到数据中的内容?我试过alert(data)
,它只显示'对象XML对象'。我知道内部数据是lesson18_test.xml,但只想看看它在数据中的结构。
答案 0 :(得分:2)
你可能会更容易找到$ .ajax的用法(我这样做,提供更多自由定制,个人喜好):
http://api.jquery.com/jQuery.ajax/
$.ajax(
{
type: "POST",
url: "/asdfasdf",
data: "foo=bar&name=cookiemonster",
// alternatively: data: { foo: "bar", name: "cookiemonster" },
timeout: 5000, /* 5sec */
success: function(data)
{
},
error: function(fa,il,ure)
{
// yay error handler
alert(fa + "\n" + il + "\n" + ure);
},
statuscode: { 404: function() { alert('Not found'); /* another way to handle errors */ }
});
答案 1 :(得分:0)
尝试alert(data.responsetext)
这将显示数据而不构造为对象
答案 2 :(得分:0)
使用jqXHR对象获取responseXML
或responseText
属性,如下所示
$(document).ready(function() {
var url = "lesson18_test.xml";
function processData(data, textStatus, jqXHR ) {
alert(jqXHR.responseText);
console.log(jqXHR.responseText)
var resultStr = "";
var items = $(data).find('language');
$(items).each(function(i)
{
resultStr += $(this).text() + '<br />';
$('#result').html(resultStr);
});
}
$('a').click(function()
{
$.get(url, processData);
});
});