var templateLink = envVars.rootTemplateFolder + $(this).attr('link');
console.log("templateLink : ", templateLink);
$.ajax({
method: "GET",
url: templateLink,
context: this,
success : function(result){
var theHtml = result.toString();
$(this).replaceWith(theHtml);
}
templateLink 应该 = "resources/spa/templates/header.html"并且根据控制台日志它是。
但我总是在“resources/spa/resources/spa/templates/header.html”上收到 404
我可以取出变量并只传递基本字符串“resources/spa/templates/header.html” 而且,我仍然收到副本。
如果只是将其保留为“/templates/header.html”,那么它将不会重复“resources/spa”,当然,字符串的其余部分现在不存在。因此,这表明传递给 AJAX 调用的对象正在被解析和更改。
我该如何阻止这个?我很困惑为什么它会这样。
有什么想法吗?
最佳答案
它没有被解析或更改。问题是你没有考虑ajax获取的路径,它被添加到你所在的路径位置。所以如果你在http://yoursite/resources/spa/templates/header.html例如,您在 Apache 中的别名是 http://yoursite/resources/spa/基本路径将添加到您的 ajax 字符串中。这可能就是问题所在。
你的浏览器的实际路径,去掉公共(public)部分。
关于javascript - AJAX 请求中作为 URL 传递的变量重复部分 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42566743/