2012-03-07 87 views
1

我正在開發一個網站,它從模板頁面加載html,然後從XML加載內容。例如在文檔中調用這些函數.ready函數谷歌瀏覽器 - 失敗GET ajax請求

$.ajax({ 
       type : "GET", 
       url : "template.html", 
       dataType : "html", 
       success : function(html) { 
        var ndoc = document.createElement('html'); 
        ndoc.innerHTML = html; 
        page = $('body', ndoc); 
        $('body').html(page.html()); 

        $.ajax({ 
         type : "GET", 
         url : "XML/content.xml", 
         dataType : "xml", 
         success : function(xml) { 
          page = $(xml).find('chisiamo').find('dialogue')[0]; 

          setupPage(page); 
         } 
        }); 

       } 
      }); 

這適用於Firefox和Safari。但在Chrome中,當它嘗試加載template.html時,我得到'原始null不被Access-Control-Allow-Origin'允許。我怎麼解決這個問題?非常感謝你。

+0

您能否顯示一個截圖? – zerkms 2012-03-07 20:44:48

+1

截圖?這是我在Chrome控制檯中得到的確切字符串:'XMLHttpRequest無法加載file:///Users/***/Dropbox/CorpoMusicale/template.html。 Origin-null不被Access-Control-Allow-Origin允許。' – Lorenzo 2012-03-07 20:46:53

+0

你見過:http://stackoverflow.com/questions/5224017/origin-null-is-not-allowed-by-access-control-allow-origin-in-chrome-why – Igor 2012-03-07 20:49:10

回答

0

嘗試使用這個參數啓動谷歌瀏覽器:

google-chrome --disable-web-security --allow-file-access-from-files 
+0

我需要在谷歌瀏覽器中使本網站在本地工作。我知道你發佈的論點,但是沒有辦法改變我的請求,所以它可以與沒有啓動選項的標準Chrome一起使用? – Lorenzo 2012-03-07 20:58:59

+0

我不認爲是因爲這是'安全'。 你有沒有嘗試把你的文件放入http服務器? – Damoun 2012-03-07 21:02:09

0

您應該能夠建立Chrome Web App和在清單中設置的權限,允許其訪問從文件中讀取文件://方案。

http://code.google.com/chrome/extensions/manifest.html

+0

你可以請更具體嗎?我真的只是接近整個兼容性的東西,而且在這種情況下非常無能。 – Lorenzo 2012-03-07 21:30:05

+0

我設法構建清單並獲得第一個HTML請求,但仍無法從XML文件加載。 – Lorenzo 2012-03-07 23:35:00

0

你必須權限添加到您在清單所請求的頁面。也可以嘗試使用$ .getJSON代替;)