javascript - AJAX 请求中作为 URL 传递的变量重复部分 url

标签 javascript jquery ajax url request

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/

相关文章:

JavaScript - setInterval 只运行一次

javascript - X-editable 不更新数据库中的值

javascript - 如何从输入字段中的json中搜索数据

jquery - Internet Explorer中跨域POST请求ajax

Javascript:从内部对象调用顶级窗口函数

javascript - 参数 'sql' 的预期类型为 utf8,但得到的类型为 'object'

javascript - 动态加载 midi.js soundfonts

javascript - 使用 JavaScript 将基本标记附加到头部

javascript - 在 "beforeunload"上使用 jQuery 调用 Rails Controller 方法

Javascript:绕过使用全局变量