Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hard-coded URL's

I have a query regarding hard-coded url's. When making a post call from JavaScript using jquery I do it as $.post('http://localhost:8000/timer/check/'

Now this works fine on my development server. When I have to deploy it on another server I have to manually change all the url's in the code. So how does one avoid hard-coding url's?

like image 778
Aniruddha Bhondwe Avatar asked Sep 08 '16 07:09

Aniruddha Bhondwe


2 Answers

For achieve this you have to use write below tag in your meta tag

<head>
<base href="{{your base url_here}}">
</head>

After this you have to write path relative to this base url.

For Example

<head>
    <base href="http://localhost:8000/timer/check/">
</head>

If full URL is http://localhost:8000/timer/check/test.php. Now you can write relative to base url 'test.php' in ajax call.

Example: https://jsfiddle.net/ptLzs7m5/1/

like image 98
Haresh Vidja Avatar answered Oct 16 '22 02:10

Haresh Vidja


try like this

 var host = window.location.hostname;
 var url = host + "/" + timer/check/;

Now you can pass the url to your post method

you can use this also

 window.location.protocol   //you'll get your protocol `http` or `https` 
 window.location.port       //this will return the port
like image 30
Shakir Ahamed Avatar answered Oct 16 '22 02:10

Shakir Ahamed