我无法使用下面的AJAX代码访问xml文件的内容。但是,当我在我的计算机上本地存储文件时尝试访问该文件时,它可以正常工作。我试图从这个xml文件中提取数据以生成我自己的倒计时。请指教。
这是我的代码:
// Reading from .xml
//
$(document).ready(function()
{
function loadfail(){
alert("Error: Failed to read file!");
}
function load_date(document){
$(document).find("countdown").each(function(){
var $date = $(this);
var $day = $date.find('startdays').text();
var $hours = $date.find('starthours').text();
var $minutes = $date.find('startminutes').text();
var $seconds = $date.find('startseconds').text();
$('.day').text($day);
$('.hours').text($hours);
$('.minutes').text($minutes);
$('.seconds').text($seconds);
$('#counter').countdown({
image: 'images/digits.png',
//startTime: '1:12:12:00'
startTime: $day + ':' + $hours + ':' + $minutes + ':' + $seconds,
});
});
}
$.ajax({
//url: 'date.xml', // name of file with our data
//url: 'http://localhost/countdown02/date.xml', // name of file with our data
url: 'http://website.com/dev_kit/date.xml', // name of file with our data
dataType: 'xml', // type of file we will be reading
success: load_date, // name of function to call when done reading file
error: loadfail // name of function to call when failed to read
});
});
答案 0 :(得分:0)
这不是jQuery对这个问题的真正回答,但是,它完成了工作。由于我在php中开发这个站点,我只是要从主机服务器复制xml文件并将其复制到本地目录以进行访问。这是我在没有卫生设施等情况下使用的解决方案。
<?php
$file = 'http://website.com/dev_kit/date.xml';
$newfile = './date.xml';
if ( copy($file, $newfile) ) {
echo "Copy success!";
}else{
echo "Copy failed.";
}
?>
非常感谢那些花时间帮助我的人。