jQuery.load 404 错误
我有两个纯 html 文件。 第一个左侧有一个树,您可以从中选择一个类别,然后根据选择在右侧显示 html 文件。 文件夹结构为/main/Pages,主文件夹包含主html,Pages文件夹包含子html。
问题:IE& Firefox - 可以工作,但不加载子页面的样式,也不执行其 document.ready 函数 Chrome - 404 错误
背后的 jQuery:
$(document).ready(function () {
$('.child').hide();
$('.parent').click(function (event) {
event.preventDefault();
$('.child', $(this).parent()).slideToggle('slow');
});
$('a').click(function(event) {
var innerText = $(this).attr('href');
var elem = $('#' + innerText);
if(elem.length > 0) {
$('#container').load("Pages/" + elem[0].innerHTML + ".html", function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#container").html(msg + xhr.status + " " + xhr.statusText + " <b>"+elem[0].innerHTML+"</b>");
}
});
}
event.preventDefault();
});
});
Chrome 标头:
请求 URL:file:///D:/PacientData/Pages/labDemandeCode1.html
选项 文件:///D:/PacientData/Pages/labDemandeCode1.html HTTP/1.1
访问控制请求方法:GET
来源:空
访问控制请求标头:来源、X-Requested-With、接受
任何有关这两个问题的帮助将不胜感激!
I have two plain html files.
The first has a tree on the left from which you select a category and then it displays on the right the html file according to the selection.
the folder structure is /main/Pages, the main folder contains the main html and the Pages folder the child htmls.
The problem: IE & Firefox - work but do not load the styles of the child pages nor execute their document.ready functions
Chrome - 404 error
The jQuery behind it all:
$(document).ready(function () {
$('.child').hide();
$('.parent').click(function (event) {
event.preventDefault();
$('.child', $(this).parent()).slideToggle('slow');
});
$('a').click(function(event) {
var innerText = $(this).attr('href');
var elem = $('#' + innerText);
if(elem.length > 0) {
$('#container').load("Pages/" + elem[0].innerHTML + ".html", function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#container").html(msg + xhr.status + " " + xhr.statusText + " <b>"+elem[0].innerHTML+"</b>");
}
});
}
event.preventDefault();
});
});
Chrome header:
Request URL:file:///D:/PacientData/Pages/labDemandeCode1.html
OPTIONS file:///D:/PacientData/Pages/labDemandeCode1.html HTTP/1.1
Access-Control-Request-Method: GET
Origin: null
Access-Control-Request-Headers: Origin, X-Requested-With, Accept
Any help regarding both problems would be highly appreciated!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您测试 AJAX 内容时,不要使用
file://
。它不会很好地工作。将您的文件放在 Web 服务器上,即使它是 Apache 服务器的本地 IIS。Don't use
file://
when you're testing AJAX stuff. It will not work very well. Put your files on a webserver, even if it is a local IIS of Apache server.如果你的网址很好(在firebug中查看..)它可能是服务器响应的标头 header("HTTP/1.1 200 OK");有时可以修复这个 404。
If your url is good (look in firebug..) it may be the header of the server's response header("HTTP/1.1 200 OK"); can fix this 404 sometimes.
您不应该通过
file:
URL 测试代码,而应该通过 HTTP,例如在本地安装网络服务器。特别是对于 AJAX,在file:
环境中工作时会出现很多问题。You shouldn't test your code through
file:
URLS but via HTTP, e.g. by installing a webserver locally. Especially with AJAX there are quite some problems when working in afile:
environment.