0

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

For example:

$(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");
                }
            }
        });
    });
});

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");
                    }
                }
            });
        });
    });

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.

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

nbanic
  • 1,270
  • 1
  • 8
  • 11
Trevor_zam
  • 592
  • 2
  • 7
  • 21
  • you can use or your second example var baseUrl to set it. No need to declare nothing in php. Or just use the full url, baseUrl is not mandatory – Timur May 07 '14 at 15:47
  • 1
    http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag – A. Wolff May 07 '14 at 15:51

3 Answers3

1

You don't need to specify the full base url.

change:

url: baseUrl + "public/account/login/",

to:

url: "/public/account/login/",

and it should continue to work.


consider the following: http://localhost/test.php

test.php

<?php

// GET Handler
if (isset($_GET['loadData']))
{
    exit('Got your request');
}

?>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">

$(document).ready(function()
{
    $.ajax({
        url: "/test.php?loadData",
        success: function(result) {
            alert(result);
        }
    });
});

</script>
Latheesan
  • 23,247
  • 32
  • 107
  • 201
  • It doesn't work. Where should I place the baseurl. As you can see I am currently declaring that as a normal string. – Trevor_zam May 07 '14 at 15:49
  • This may not work if the `baseURL` is something like `http://www.websiteurl.com/test/`. – John Bupit May 07 '14 at 15:51
  • And to be honest I don't see any real difference between declaring full url and baseurl + extension. – Trevor_zam May 07 '14 at 15:51
  • I've updated my answer with example scenario. It does work without specifying the base url and using `/` (site root path) – Latheesan May 07 '14 at 15:54
  • I have never used fully-qualified URLs in AJAX requests unless I need content from another domain. As long as you want content from the same domain that is running the script, you can use relative URLs as this answer is demonstrating. – Cypher May 07 '14 at 16:08
0

I am developing a large application and have quickly learned that there are great benefits to keeping your path in one location and using variables and/or constants everywhere else. The reason is pretty simple: Flexibility, DRY, etc.

If you have a PHP application that also uses Javascript, as I do, you may want to consider doing something like this:

File: constants.php

<?php
define("WEB_ADDRESS","http://www.somewebsite.com");
//...
?>

And now, to put it into every JS page, while I do not know your specific architecture and whether you are using a bootstrap or not, I assume you have one or more files from which you bootstrap and load headers/footers, or you use a template program. If not, you may want to look into it. In any case, the concept is that you simply declare it at the start of your page text for each page you load before including any other JS.

File: header.php (called in your web page you are loading).

//No Javascript above.
echo '<script type="text/javascript">var WEB_ADDRESS = ' . WEB_ADDRESS . '</script>';
//Now you can put in your Javascript.

The end purpose is that each Javascript file you include or any Javascript you call after this point is going to have access to the variable WEB_ADDRESS. If you need to change that path for any reason you can easily do so without breaking any links, in PHP and Javascript.

Community
  • 1
  • 1
smcjones
  • 5,490
  • 1
  • 23
  • 39
0

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>

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'
dav
  • 8,931
  • 15
  • 76
  • 140
  • Might be silly question but which part of your code do I replace it with my base url for example if my baseurl is "www.aaa.com/user". – Trevor_zam May 07 '14 at 16:30
  • @Trevor_zam I updated a little the answer, if ur base url contains smth more than the host name, than u should add that, so it will be `` – dav May 07 '14 at 16:46