JQuery Ajax通过Handler访问外部XML数据的代码(2)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="https://www.jb51.net/jquery-1.3.2.min.js"></script>
</head>
<body>
<div>
<span>loading...</span>
</div>
<script type="text/javascript">
function getData() {
$("#loader").ajaxStart(function() {
$(this).show();
});
$("#loader").ajaxComplete(function() {
$(this).hide();
});
$.ajax({
type: "GET",
url: "Stock.ashx?u=http://money.service.msn.com/StockQuotes.aspx?symbols=msft",
dataType: "xml",
success: function(data) {
var last = "";
var change = "";
var percentchange = "";
var volume = "";
var cap = "";
var yearhigh = "";
var yearlow = "";
$(data).find('ticker').each(function() {
last = $(this).attr('last');
change = $(this).attr('change');
percentchange = $(this).attr('percentchange');
volume = $(this).attr('volume');
cap = $(this).attr('marketcap');
yearhigh = $(this).attr('yearhigh');
yearlow = $(this).attr('yearlow');
document.getElementById('con').innerHTML = '<span>name:' + last + '&nbsp;&nbsp;&nbsp;&nbsp;high:' + volume + '&nbsp;&nbsp;&nbsp;&nbsp;low:' + cap + '</span>';
})
}
});
}
$(window).load(getData);
</script>
</body>
</html>


下面是实现的结果:
name:25.8 high:67,502,221 low:$226,107,039,514
  Handler的写法基本都大同小异,因此可以写成一个通用的例子,以后如遇到在脚本中需要跨域访问资源时便可以直接使用!代码记录于此,方便查阅。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdxxpp.html