如何使用jQuery加载本地文件? (与文件://) [英] How can I load a local file using jQuery? (with file://)

查看:911
本文介绍了如何使用jQuery加载本地文件? (与文件://)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用jQuery从数据文件(例如JSON .js文件)中加载数据?

Is there any way to load data from a data file (eg a JSON .js file) using jQuery?

例如:

$.get("file:///C:/objectData.js", function() { alert('Load was performed.'); });

目前,JQuery似乎没有尝试执行简单的HTTP GET,而是尝试对其执行OPTIONS请求,该请求在file://URI上失败.我只想加载数据,以便可以在脱机环境(未安装Web服务器)中使用该站点.

At the moment, instead of doing a simple HTTP GET, JQuery appears to be trying to do an OPTIONS request on it, which fails on a file:// URI. I just want to load the data in so that the site can be used in an offline environment (without a web server installed).

推荐答案

GET需要HTTP连接,因此,如果没有本地Web服务器,它将无法正常工作.虽然您可以打开并读取文件使用HTML5,您将无法以这种方式加载JavaScript资源.

GET requires an HTTP connection, so without a local web servber it won't work. While you can open and read a file using HTML5, you can't load a JavaScript resource that way.

如果页面是在本地加载的,通常会使用脚本标签加载JS.

If the page is loaded locally, you'd usually load the JS using a script tag.

<script type='text/javascript' src='/objectData.js'></script>

解决此问题的唯一方法可能是在以下答案中:

The only way around this may be in this answer: Is it possible to load in a local version of a JavaScript file instead of the server version?

(都需要创建本地伪服务器):

or this (both require making a local pseudo-server) :

这篇关于如何使用jQuery加载本地文件? (与文件://)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆