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

2024-02-01

我有一个非常简单的测试页面 http://issues.tauren.com/testjson/index.html它通过 jQuery 的 $.getJSON 和 $.ajax 方法使用 XHR 请求。同一页面在某些情况下有效,但在其他情况下则无效。具体来说,它在 Ubuntu 上的 Chrome 中不起作用。

我正在使用 Chrome 5.0.342.7 beta 的 Ubuntu 9.10 和使用 Chrome 5.0.307.9 beta 的 Mac OSX 10.6.2 进行测试。

  • 当文件从 Ubuntu/Chrome 和 Mac/Chrome (在这里尝试一下 http://issues.tauren.com/testjson/index.html).
  • 当文件安装在 Mac/Chrome 的本地硬盘上时(通过 file:///... 访问),它可以正常工作。
  • It FAILS当文件安装在 Ubuntu/Chrome 中的本地硬盘上时(使用 file:///... 访问)。

可以从此处以 tar/gzip 文件形式下载这一小组 3 个文件:http://issues.tauren.com/testjson/testjson.tgz 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

当它不起作用时,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

请注意,尽管成功处理程序已运行,但它甚至不显示 XHR 请求。我发誓这之前在 Ubuntu/Chrome 中可以正常工作,并且担心出现问题。我已经卸载并重新安装了 Chrome,但这没有帮助。

有人可以在你的 Ubuntu 系统上本地尝试一下并告诉我是否有任何问题吗?请注意,它在 Firefox 中似乎运行良好。


另一种方法是在您的目录上启动本地 HTTP 服务器。在安装了 Python 的 Ubuntu 和 MacO 上,这是一句简单的话。

转到包含 Web 文件的目录,然后:

python -m SimpleHTTPServer

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐