gpt4 book ai didi

javascript - 如何使用 jquery 和 ajax 读取、解析和显示 xml

转载 作者:行者123 更新时间:2023-11-30 16:50:10 25 4
gpt4 key购买 nike

我正在尝试使用 jquery 和 ajax 读取、解析和显示 xml 文件。但是在尝试这样做时我收到了一个错误,因此我无法在

这是我的代码。

<!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 runat="server">
<script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>

<script type="text/javascript" language="javascript">
$(document).ready(function(){
$("#dvContent").append("<ul></ul>");
$.ajax({
type: "GET",
url: "http://localhost/BookList.xml",
dataType: "xml",
success: function(xml){
$(xml).find('Book').each(function(){
var sTitle = $(this).find('Title').text();
var sPublisher = $(this).find('Publisher').text();
$("<li></li>").html(sTitle + ", " + sPublisher).appendTo("#dvContent ul");
});
},
error: function() {
alert("An error occurred while processing XML file.");
}
});
});
</script>
<style type="text/css">
body
{
font-family : Arial;
font-size : 10pt;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="dvContent">

</div>
</form>

我的 xml 文件如下所示

<?xml version="1.0" encoding="utf-8"?>
<BookList>
<Book>
<Title>jQuery: Novice to Ninja</Title>
<Publisher>Site point</Publisher>
</Book>
<Book>
<Title>Learning jQuery</Title>
<Publisher>PACKT</Publisher>
</Book>
<Book>
<Title>Head First jQuery</Title>
<Publisher>O'Reilly</Publisher>
</Book>
<Book>
<Title>jQuery UI 1.8</Title>
<Publisher>PACKT</Publisher>
</Book>
</BookList>

我得到的错误是

XMLHttpRequest cannot load http://localhost/booklist.xml. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

现在我不知道如何为 xml 文件添加 access-control-allow-origin。如果它是 php,我可以做到,但我被卡住了。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com