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

Creating portable JS functions containing ajax url calls

100+
P: 228
Alo,

Hope my title is clear...am part of a team that works with CodeIgniter. The HTTP links are in this fashion: www.abc.com/content www.abc.com/content/edit/2

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) {
  11.  
  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 www.abc.com/tools/getcname

It works for those urls that are www.abc.com/content format as it the URL is correctly appended. For other format links, it doesn't work as it tries abc.com/content/edit/tools/getcname

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


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

100+
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

gits
Expert Mod 5K+
P: 5,320
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

omerbutt
100+
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
www.abc.com domain then it would send the request to www.abc.com/tools/getcname and if your other department access this application from www.test.com then it would send request to www.test.com/tools/getcname

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

Post your reply

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