gpt4 book ai didi

javascript - jQuery 的 `getScript` 失败。路径正确,脚本下载正确。

转载 作者:行者123 更新时间:2023-11-30 00:16:35 26 4
gpt4 key购买 nike

我使用 Mustache.js 编写了一个网站。模板通过 jQuery 使用 AJAX 加载。

当我在本地运行网站并进行测试时,一切都加载正常。然而,当我将站点上传到我的学校服务器时,jQuery 的 getScript 报告加载 mustache.min.js 失败。

每当我需要运行渲染模板时,我都会使用以下辅助函数:

doWithMustache: function(f) {
$.getScript("./js/mustache.min.js", function() {
f();

}).fail(
console.error("Failed to fetch mustache script.");
);
}

我正在使用它:

renderPage: function(uniqueBodyElement, lastModifiedDate) {
var data;
var headTemplate;
var bodyTemplate;
var articleTemplate;

//Wait until all data is available
$.when(
$.get("./templates/siteData.json", function(d){ data = d }),
$.get("./templates/HeadTemplate.mustache", function(hT){ headTemplate = hT }),
$.get("./templates/BodyTemplate.mustache", function(bT){ bodyTemplate = bT }),
$.get("./templates/ArticleTemplate.mustache", function(aT){ articleTemplate = aT })

).done(function() {
Helpers.doWithMustache(function() {
var partial = TemplateLoader.getTemplatePartial(uniqueBodyElement);
partial.lastModifiedDate = lastModifiedDate;

var renderedHead = Mustache.render(headTemplate, data);
var renderedBody = Mustache.render(bodyTemplate, data, partial);

var renderedArticleBody = Mustache.render(articleTemplate, data, { articleBody: renderedBody });

$('head').append(renderedHead);
$('body').html(renderedArticleBody);

console.log("Templates Loaded.");
});

}).fail(function() {
console.error("Failed to fetch templates or site data.")
});

}

在本地,这按预期工作。然而,上传后,我收到控制台错误,告诉我它“无法获取 mustache 脚本”。不幸的是,fail() 似乎没有报告任何错误,所以我不知道是什么导致了这个问题。

明显的问题是路径错误,但是当我在“开发者工具”中检查 Edge 的“网络”选项卡时,包括 mustache 脚本在内的所有内容都被正确加载:

enter image description here

我还通过将加载路径更改为我知道错误的路径来验证这一点。我不仅遇到了获取失败的错误,而且还遇到了“Bad URI”错误。

所以,我知道路径是正确的,但它仍然不会加载脚本。

有谁知道问题可能是什么,或者如何更接近于找到它?

最佳答案

您忘记了匿名函数引用,现在您总是运行console.log 并将结果返回给fail() (这将是 undefined)。

改成

doWithMustache: function(f) {
$.getScript("./js/mustache.min.js", function() {
f();
}).fail(function() {
console.error("Failed to fetch mustache script.");
});
}

关于javascript - jQuery 的 `getScript` 失败。路径正确,脚本下载正确。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34476482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com