在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.");
    }
});

最佳答案

他们不是the same domain。同源策略要求域完全匹配。一个网址具有www.,如果它提供相同的确切站点,则本质上使其成为不同的域。从您的AJAX调用使用的URL中删除www

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

08-19 14:59