在Chrome中使用本地文件的jQuery getJSON问题

Tau*_*ren 48 ajax jquery json google-chrome

我有一个非常简单的测试页面,它使用jQuery的$ .getJSON和$ .ajax方法的XHR请求.同一页面在某些情况下有效,而在其他情况下则无效.特别是,它在Ubuntu上的Chrome中不起作用.

我正在使用Chrome 5.0.342.7测试版测试Ubuntu 9.10,使用Chrome 5.0.307.9测试版测试Mac OSX 10.6.2.

  • 当文件从Ubuntu/Chrome和Mac/Chrome安装在Web服务器上时,它可以正常工作(在此处试试).
  • 当文件安装在Mac/Chrome的本地硬盘驱动器上时(使用file:/// ...访问),它可以正常工作.
  • 当文件安装在Ubuntu/Chrome中的本地硬盘驱动器上时(文件:/// ...访问),它会失败.

可以从这里下载tar/gzip文件中的一小组3个文件:http: //issues.tauren.com/testjson/testjson.tgz

当它工作时,Chrome控制台会说:

XHR finished loading: "http://issues.tauren.com/testjson/data.json".
index.html:16Using getJSON
index.html:21
Object
result: "success"
__proto__: Object
index.html:22success
XHR finished loading: "http://issues.tauren.com/testjson/data.json".
index.html:29Using ajax with json dataType
index.html:34
Object
result: "success"
__proto__: Object
index.html:35success
XHR finished loading: "http://issues.tauren.com/testjson/data.json".
index.html:46Using ajax with text dataType
index.html:51{"result":"success"}
index.html:52undefined
Run Code Online (Sandbox Code Playgroud)

如果它不起作用,Chrome控制台将显示以下内容:

index.html:16Using getJSON
index.html:21null
index.html:22Uncaught TypeError: Cannot read property 'result' of null
index.html:29Using ajax with json dataType
index.html:34null
index.html:35Uncaught TypeError: Cannot read property 'result' of null
index.html:46Using ajax with text dataType
index.html:51
index.html:52undefined
Run Code Online (Sandbox Code Playgroud)

请注意,尽管运行了成功处理程序,它甚至不显示XHR请求.我发誓以前在Ubuntu/Chrome上工作过,我担心有些事搞砸了.我已经卸载并重新安装了Chrome,但这没有帮助.

有人可以在你的Ubuntu系统上本地试用,并告诉我你是否有任何麻烦?请注意,它似乎在Firefox中正常工作.

Séb*_*rra 38

另一种方法是在目录上启动本地HTTP服务器.在安装了Python的Ubuntu和MacO上,它是一个单行程序.

转到包含您的Web文件的目录,并:

python -m SimpleHTTPServer
Run Code Online (Sandbox Code Playgroud)

然后使用任何Web浏览器连接到http:// localhost:8000/index.html以测试您的页面.

  • 在Python 3上,它是`python -m http.server`.此外,它在Windows中也可以正常工作. (2认同)

Dan*_*rer 30

这是Chrome的一个已知问题.

这是bug跟踪器中的链接:

问题40787:本地文件未加载Ajax


小智 13

在Windows上,Chrome可能会安装在AppData文件夹中:

"C:\用户\\应用程序数据\本地\谷歌\镀铬\应用程序"

在执行命令之前,请确保所有Chrome窗口都已关闭,否则无法正常运行.或者,命令行参数无效.

chrome.exe --allow-file-access-from-files
Run Code Online (Sandbox Code Playgroud)