2012-09-22 22 views
0

可能重複:
Circumventing Chrome Access-control-allow-origin on the local file system?

我的問題是非常簡單的,我使用$就功能來讀取一個CSV文件和代碼工作FF和IE9,但不是在Chrome ...

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<script src="http://code.jquery.com/jquery-1.8.2.min.js"> 
</script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $.ajax({ 
     type: "GET", 
     url: "autorating.csv", 
     contentType: "text/csv", 
     success: function(data) {alert('asdsa');} 
    }); 
}); 


</script> 
<title>Untitled Document</title> 
</head> 

<body> 
</body> 
</html> 

任何幫助將明顯.. 謝謝

+0

腳本控制檯說什麼?任何錯誤報告? –

+0

也console.log(數據)在您的成功處理程序和瀏覽器之間進行比較。 –

+1

XMLHttpRequest無法加載file:/// F:/shane/autorating.csv。 Access-Control-Allow-Origin不允許Origin null。 –

回答

3

您正在閱讀沒有服務器的本地文件。 Chrome不允許這樣做。

This Link供進一步閱讀。

使用--allow-file-access-from-files作爲更安全的解決方法。

Reference :: Issue in Chrome

+0

你有什麼參考嗎? –

+0

我已經分享了較舊的帖子的鏈接。 –

+0

謝謝Ashish ... :) –

-1

的Content-Type必須包含的字符集

Content-Type: text/plain; charset=utf-8

+0

謝謝,但還是不行... :( –

+1

text/csv is correct .. http://stackoverflow.com/a/393696/1085285 –

2

XMLHttpRequest cannot load file:///F:/shane/autorating.csv. Origin null is not allowed by Access-Control-Allow-Origin.意味着你要加載它在鉻禁止本地文件。這是有道理的,因爲你不希望任何腳本通過你所有的本地文件。 除此之外,即使您只需要在HTTP服務器上運行它,也有一種方法。看看這個this.