2016-04-29 89 views
0

在chrome開發工具中,我收到錯誤「XMLHttpRequest無法加載https://www.carcraft.atsbusinessandgames.com/xmls/carcraft_1-7-10Test.xml。請求的資源上沒有'Access-Control-Allow-Origin'標頭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."); 
    } 
}); 

回答

4

他們不是the same domain。同源政策要求完全匹配的域名。一個網址有www.,這基本上使它成爲一個不同的域名,如果它服務於同一個確切的網站。從您的AJAX調用使用的網址中刪除www

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

哇,我覺得這麼失去理智。謝謝!定時器允許我也將標記爲接受。 – carstorm

相關問題