By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,677 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,677 IT Pros & Developers. It's quick & easy.

Creating portable JS functions containing ajax url calls

P: 228

Hope my title is part of a team that works with CodeIgniter. The HTTP links are in this fashion:

Now, we have some javascsript ajax functions that are shared thru out various sections of the application. One is this:

Expand|Select|Wrap|Line Numbers
  1. function getXtime() {
  2.         var result = "";
  3.         $.ajax({
  4.             type: "GET",
  5.             async: false,
  6.             url: "../tools/getcname",
  7.             data: "{}",
  8.             contentType: "application/json; charset=utf-8",
  9.             dataType: "text",
  10.             success: function (msg) {
  12.                 result = msg;
  13.             },
  14.             error: function (jqXHR, textStatus, errorThrown) {
  15.                 result="";
  16.             }
  17.         });
  18.        //alert("returnValue"+paramList);
  19.         return result;
  20.     }
The absolute URL link is

It works for those urls that are format as it the URL is correctly appended. For other format links, it doesn't work as it tries

We don't want to use absolute URLS as the application will be used in various departments with varioius urls but same website structure.

Is there a short way to solve this?
Dec 21 '15 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 10K+
P: 12,427
You're not using an absolute URL. This: ../tools/getcname, is a relative URL.
Dec 21 '15 #2

P: 228
Yes Rabbit, we want relative except it is not working. Absolute paths are uncomfortable as some departments use a different domain and that means for each dept or institution, we have to manually edit the JS before distribution.
Dec 21 '15 #3

Expert Mod 5K+
P: 5,389
so basically it always should translate to:

Expand|Select|Wrap|Line Numbers
  1. [domain]/tools/getcname
? in that case u could use:

Expand|Select|Wrap|Line Numbers
  1. window.location.origin + '/tools/getcname'
if that was what u looking for? in the request spec in JavaScript.
Jan 7 '16 #4

P: 638
first of all from your code i can see that you are using relative urls for ajax calls not absolute .

Secondly when you say
the application will be used in various departments with varioius urls but same website structure.
i assume that you are trying to say that your application is not hosted on the web and is accessible within the intranet or the local network only, and you have this application hosted on different computers for all the departments and all of them use a separate domain name ?

If Yes then continue reading below

Actually you do not need to be concerned about the domain name you could just remove the
Expand|Select|Wrap|Line Numbers
  1. ../
from your url parameter and what ever domain you or other department is using it will automatically resolve to it if you provide your url parameter as
Expand|Select|Wrap|Line Numbers
  1.  url: "/tools/getcname",
, in this way if you are using domain then it would send the request to and if your other department access this application from then it would send request to

Hope i made it clear.
Omer Aslam
Jan 14 '16 #5

Post your reply

Sign in to post your reply or Sign up for a free account.