【发布时间】:2015-05-05 07:43:13
【问题描述】:
我正在尝试使用以下代码将文本从 txt 文档加载到 div:
$(document).ready(function(){
$('button').click(function(){
$('#contenthere').load('Load.txt');
});
});
html:
<button> click me! </button>
<div id="contenthere">//Load.txt should load here!</div>
txt:
If this text is loaded then you've managed to succefully use jQuery!
点击功能正在工作,但加载功能根本不工作,我尝试了不同的方法来解决这个问题但没有成功,并且 Load.txt 文件与索引文件位于同一文件夹中是 jquery 加载代码已存储!
编辑: 这是我得到的错误:
XMLHttpRequest cannot load file:///C:/Users/nti/Google%20Drive/Gr%C3%A4nssnittsdesign/slutUppgift/exempel/Load.txt. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
我也试过了:
$(document).ready(function(){
$('button').click(function(){
$.ajax({
url: "Load.txt",
dataType: "text",
succes : function (data){
$("#contenthere").html(data);
}
});
});
});
得到了和上面一样的错误。
【问题讨论】:
-
@preyerguilt
console有什么错误 -
您不能使用来自
file://的AJAX(尽管这可能适用于某些旧浏览器)。你需要一个http://环境才能工作,这意味着从本地主机或你的服务器调用Load.txt。 -
我已经测试了你的代码,它工作正常,请确保你有正确的权限来读取文件。另外,检查控制台输出的内容。
-
@ozil 我在问题编辑中写了错误
标签: javascript jquery click load