gpt4 book ai didi

javascript - 在 CodeIgniter 的外部 JS 文件中复制 base_url

转载 作者:行者123 更新时间:2023-12-02 16:08:11 27 4
gpt4 key购买 nike

我的外部 js 文件中有一个按钮,单击该按钮时,我希望将其发送到我拥有的 Controller 。当我在 JQuery 中将 on click 函数设置为我的本地主机时,它工作正常,但是我如何设置它,以便它的工作方式类似于 CodeIgniter 中的 base_url 工作方式,以便它可以在任何环境中工作?我尝试过使用 window.location.origin 或 window.location.host 而不是 localhost,但都不起作用。有什么想法吗?

我的外部 JS 按钮:

var html = '<div class="alert alert-success" role="alert"><p class="instruction_text text-center">' + completedText + '</p></div><input id="next_task" class="btn btn-lg btn-default text-center" style="margin-top: 150px; font-weight: bold;" type="button" value="' + text[lang]['continue'] + '"data-url="http://localhost:8888/oda/sample/instructions/' + task_id + '/' + lang + '/' + type + '">';

$('#final_message').append(html);

$(document).on('click', '#next_task', function(e) {
e.preventDefault();
location.href = $(this).data('url');
});

谢谢!

最佳答案

您可以将该工作留给浏览器。如果页面位于同一域内,则无需将其详细地放在代码中,您可以放置​​类似的内容:

'"data-url="/oda/sample/instructions/' + task_id + '/' + lang + '/' + type + '"'

请注意,开头的 / 表示它将始终到达同一个位置,忽略当前的 url。

但是,如果您出于某种原因需要编写完整的 url,window.location.origin 将不包含您的 url 的端口部分,则必须包含 window.location。 port 也使其适用于您的环境。这在生产中并不是必须要做的事情。如果您将 http:/// 放在字符串上,window.location.host 应该可以工作:

'"data-url="http://' + window.location.host + '/oda/sample/instructions/' + task_id + '/' + lang + '/' + type + '"'

关于javascript - 在 CodeIgniter 的外部 JS 文件中复制 base_url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30510194/

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