在 Chrome 中使用本地文件的 jQuery getJSON 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2541949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems with jQuery getJSON using local files in Chrome
提问by Tauren
I have a very simple test pagethat uses XHR requests with jQuery's $.getJSON and $.ajax methods. The same page works in some situations and not in others. Specificially, it doesn't work in Chrome on Ubuntu.
我有一个非常简单的测试页面,它使用带有 jQuery 的 $.getJSON 和 $.ajax 方法的 XHR 请求。同一页面在某些情况下有效,而在其他情况下无效。具体来说,它在 Ubuntu 上的 Chrome 中不起作用。
I'm testing on Ubuntu 9.10 with Chrome 5.0.342.7 beta and Mac OSX 10.6.2 with Chrome 5.0.307.9 beta.
我正在使用 Chrome 5.0.342.7 beta 的 Ubuntu 9.10 和使用 Chrome 5.0.307.9 beta 的 Mac OSX 10.6.2 进行测试。
- It works correctly when files are installed on a web server from both Ubuntu/Chrome and Mac/Chrome (try it out here).
- It works correctly when files are installed on local hard drive in Mac/Chrome (accessed with file:///...).
- It FAILSwhen files are installed on local hard drive in Ubuntu/Chrome (access with file:///...).
- 当文件从 Ubuntu/Chrome 和 Mac/Chrome 安装在 Web 服务器上时,它可以正常工作(请在此处尝试)。
- 当文件安装在 Mac/Chrome 的本地硬盘驱动器上时,它可以正常工作(通过 file:///... 访问)。
- 当文件安装在 Ubuntu/Chrome 的本地硬盘驱动器上时它失败(通过 file:///... 访问)。
The small set of 3 files can be downloaded in a tar/gzip file from here: http://issues.tauren.com/testjson/testjson.tgz
可以从这里下载 tar/gzip 文件中的 3 个小文件集:http: //issues.tauren.com/testjson/testjson.tgz
When it works, the Chrome console will say:
当它工作时,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
When it doesn't work, the Chrome console will show this:
当它不起作用时,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
Notice that it doesn't even show the XHR requests, although the success handler is run. I swear this was working previously in Ubuntu/Chrome, and am worried something got messed up. I already uninstalled and reinstalled Chrome, but that didn't help.
请注意,尽管成功处理程序已运行,但它甚至不显示 XHR 请求。我发誓这以前在 Ubuntu/Chrome 中工作过,我担心有些事情搞砸了。我已经卸载并重新安装了 Chrome,但这没有帮助。
Can someone try it out locally on your Ubuntu system and tell me if you have any troubles? Note that it seems to be working fine in Firefox.
有人可以在您的 Ubuntu 系统上本地试用并告诉我您是否有任何问题吗?请注意,它似乎在 Firefox 中运行良好。
采纳答案by Daniel Furrer
This is a known issue with Chrome.
这是 Chrome 的一个已知问题。
Here's the link in the bug tracker:
这是错误跟踪器中的链接:
回答by Sébastien RoccaSerra
Another way to do it is to start a local HTTP server on your directory. On Ubuntu and MacOs with Python installed, it's a one-liner.
另一种方法是在您的目录上启动本地 HTTP 服务器。在安装了 Python 的 Ubuntu 和 MacO 上,它是单行的。
Go to the directory containing your web files, and :
转到包含您的 Web 文件的目录,然后:
python -m SimpleHTTPServer
Then connect to http://localhost:8000/index.htmlwith any web browser to test your page.
然后使用任何 Web 浏览器连接到http://localhost:8000/index.html以测试您的页面。
回答by Thomas
On Windows, Chrome might be installed in your AppData folder:
在 Windows 上,Chrome 可能安装在您的 AppData 文件夹中:
"C:\Users\\AppData\Local\Google\Chrome\Application"
“C:\Users\\AppData\Local\Google\Chrome\Application”
Before you execute the command, make sure all of your Chrome windows are closedand not otherwise running. Or, the command line param would not be effective.
在执行该命令之前,请确保所有 Chrome 窗口都已关闭且未以其他方式运行。或者,命令行参数将无效。
chrome.exe --allow-file-access-from-files
回答by Zdeněk Ml?och
You can place your json to js file and save it to global variable. It is not asynchronous, but it can help.
您可以将 json 放入 js 文件并将其保存到全局变量。它不是异步的,但可以提供帮助。
回答by Luke Dohner
This code worked fine with sheet.jsonlocally with browser-sync as the local server. -But when on my remote server I got a 404 for the sheet.json file using Chrome. It worked fine in Safari and Firefox. -Changed the name sheet.json to sheet.JSON. Then it worked on the remote server. Anyone else have this experience?
此代码与 sheet.jsonlocally 一起使用,浏览器同步作为本地服务器运行良好。- 但是当在我的远程服务器上时,我使用 Chrome 得到了 sheet.json 文件的 404。它在 Safari 和 Firefox 中运行良好。-将名称 sheet.json 更改为 sheet.JSON。然后它在远程服务器上工作。其他人有这种经历吗?
getthejason = function(){
var dataurl = 'data/sheet.JSON';
var xhr = new XMLHttpRequest();
xhr.open('GET', dataurl, true);
xhr.responseType = 'text';
xhr.send();
console.log('getthejason!');
xhr.onload = function() {
.....
}
回答by Shazron
@Mike On Mac, type this in Terminal:
@Mike 在 Mac 上,在终端中输入:
open -b com.google.chrome --args --disable-web-security
回答by CommissarXiii
An additional way to get around the problem is by leveraging Flash Player's Local Only security sandbox and ExternalInterface methods. One can have JavaScript request a Flash application published using the Local Only security sandbox to load the file from the hard drive, and Flash can pass the data back to JavaScript via Flash's ExternalInterface class. I've tested this in Chrome, FF and IE9, and it works well. I'd be happy to share the code if anyone is interested.
解决该问题的另一种方法是利用 Flash Player 的 Local Only 安全沙箱和 ExternalInterface 方法。可以让 JavaScript 请求使用 Local Only 安全沙箱发布的 Flash 应用程序从硬盘加载文件,Flash 可以通过 Flash 的 ExternalInterface 类将数据传回 JavaScript。我已经在 Chrome、FF 和 IE9 中对此进行了测试,效果很好。如果有人感兴趣,我很乐意分享代码。
EDIT:I've started a google code (ironic?) project for the implementation: http://code.google.com/p/flash-loader/
编辑:我已经开始了一个谷歌代码(讽刺?)项目的实施:http: //code.google.com/p/flash-loader/