如何设置基本URL,该在哪里声明? [英] How to setup a base URL and where do I declare it?

查看:62
本文介绍了如何设置基本URL,该在哪里声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对应该在哪里设置基本URL感到困惑.目前,我正在按原样称呼URL及其扩展名:

I am confused as to where I should setup a base URL. Currently I am calling the URL and its extensions as they are:

例如:

$(document).on("pageinit", "#login", function () {
    $("#form2").on("submit", function (event) {
        $.ajax({
            type: "GET",
            url: "http://www.websiteurl.com/public/account/login/", //the full url
            data: $("#form2").serialize(),
            success: function (data) {
                if (data.loggedIn) {
                    alert("logged");
                } else {
                    alert("not logged");
                }
            }
        });
    });
});

根据我的理解,基本URL是一个字符串,我只是将信息连接到该字符串,例如:

From my understanding base URL is a string and I just concatenate information to that string such as:

var baseUrl = "http://www.websiteurl.com/";

$(document).on("pageinit", "#login", function () {
        $("#form2").on("submit", function (event) {
            $.ajax({
                type: "GET",
                url: baseUrl + "public/account/login/", //using baseUrl
                data: $("#form2").serialize(),
                success: function (data) {
                    if (data.loggedIn) {
                        alert("logged");
                    } else {
                        alert("not logged");
                    }
                }
            });
        });
    });

但是我相信我做错了,一些网站谈论使用PHP声明baseurl.我可以从中获得一些建议,以及应该在哪里声明基本URL字符串.

But I believe I am doing it wrong and some sites talk about using PHP to declare baseurl. can I get some advice on how to do that and where should I declare the base URL string.

是HTML页面上的< base> 还是js文件上的 String ?谢谢.

As a <base> at the HTML page or as a String on the js file? Thanks.

推荐答案

我不确定,但是据我了解,就像

I am not sure, but as far as I understand,it is like

<script type="text/javascript">
var baseUrl = '<?php echo "http://".$_SERVER["HTTP_HOST"]."/"?>';
</script>

此处 $ _ SERVER ["HTTP_HOST"] 部分是主机名,例如 www.example.com 然后在您的Ajax请求中,您可以使用该 baseUrl js var并可以连接您的url

here the $_SERVER["HTTP_HOST"] part is the host name, say, www.example.com then in your ajax request you can use that baseUrl js var and can concatenate your url

url: baseUrl + 'account/login'

这篇关于如何设置基本URL,该在哪里声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆