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?
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/
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With