访问控制允许来源,即使在同一域上

Access-Control-Allow-Origin even on same domain

本文关键字:访问控制      更新时间:2023-09-26

在chrome开发工具中,我收到错误"XMLHttpRequest无法加载https://www.carcraft.atsbusinessandgames.com/xmls/carcraft_1-7-10Test.xml.请求的资源上不存在"Access Control Allow Origin"标头。原点'https://carcraft.atsbusinessandgames.com因此不允许访问。"

当两个文件都在同一个域上时,为什么会出现此错误?

如果您需要,这里是适用的代码:XML(页面将为空白,右键单击并查看源代码):https://carcraft.atsbusinessandgames.com/xmls/carcraft_1-7-10Test.xml

将其放入HTML页面的部分:

                <article>
                    <header>
                    </header>
                    <br />
                    <br />
                    <table id="ModsList">
                        <tr style="font-weight: bold;">
                        <td>Mod Name</td>
                        <td>Author(s)</td>
                        <td>Version</td>
                        <td>Date added/updated</td>
                        <td>Description</td>
                        </tr>
                    </table>
                    <script src="https://www.code.jquery.com/jquery-1.8.1.min.js"></script>
                    <script src="/test.js"></script>
                </article>

这是javascript/jquery:

$.ajax({
    url: 'https://www.carcraft.atsbusinessandgames.com/xmls/carcraft_1-7-10Test.xml',
    type: "Get",
    dataType: 'xml',
    success: function (result) {
        $(result).find('Module').each(function() {
             var name = $(this).attr("name");
            var url = $(this).find('url').text();
            var authors = $(this).find('authors').text();
            var version = $(this).find('version').text();
            var date = $(this).find('date').text();
            var description = $(this).find('description').text();
            $("#ModsList").append("<tr>" + "<td>" + "<a href=" + url + ">" + name + "</a>" + "</td>" + "<td>" + authors + "</td>" + "<td>" + version + "</td>" + "<td>" + date + "</td>" + "<td>" + description + "</td>" + "</tr>");
        });
    },
    failure: function() {
    alert("Notify the site owner that the xml file is unreadable.");
    }
});

它们不是同一个域。同源策略要求域的完全匹配。一个url有www.,如果它服务于同一个确切的网站,它本质上是一个不同的域名。从AJAX调用使用的url中删除www

https://www.carcraft.atsbusinessandgames.com
https://carcraft.atsbusinessandgames.com