Creating portable JS functions containing ajax url calls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samvb
    New Member
    • Oct 2006
    • 228

    Creating portable JS functions containing ajax url calls

    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:

    Code:
    function getXtime() {
            var result = "";
            $.ajax({
                type: "GET",
                async: false,
                url: "../tools/getcname",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "text",
                success: function (msg) {
                    
                    result = msg;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    result="";
                }
            });
           //alert("returnValue"+paramList);
            return result;
        }
    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?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You're not using an absolute URL. This: ../tools/getcname, is a relative URL.

    Comment

    • samvb
      New Member
      • Oct 2006
      • 228

      #3
      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.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        so basically it always should translate to:

        Code:
        [domain]/tools/getcname
        ? in that case u could use:

        Code:
        window.location.origin + '/tools/getcname'
        if that was what u looking for? in the request spec in JavaScript.

        Comment

        • omerbutt
          Contributor
          • Nov 2006
          • 638

          #5
          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
          Code:
          ../
          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
          Code:
           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

          Comment

          Working...