0

I need some solution to give relative path in my jquery ajax call as of now I am using soem thing like ../ . Any suggestions would be higly appreaciated.

Code

$.ajax({
        type: 'get',
        url: '../../MyPage/getDetails',
        success: function (data) {
            if (data > 0) {
4
  • 2
    The path should be relative to the url you see in browser not to the javascript file
    – Imdad
    Commented May 22, 2012 at 18:13
  • Are you sure the path ends with getDetails i.e. without any extension?
    – Imdad
    Commented May 22, 2012 at 18:14
  • Imdad getDetails is a controller method
    – Kurkula
    Commented May 22, 2012 at 19:09
  • OK, just confirming if it is not a silly mistake. sometimes it happens.
    – Imdad
    Commented May 23, 2012 at 13:18

3 Answers 3

1

This is a bit of a hack, but you are client side so :)

This code makes some assumptions - like that the script tage for myscript.js exists

Assume you have some markup on your page like so:\

<script src='../js/myscript.js' type="text/javascript"></script>
<div>hiya</div>

OK, now we will use the script tag and get the base url from that :)

Let's create a Util class we can use, add a get base url method and alert that value:

Working copy here: http://jsfiddle.net/SmCLy/

function Util() { /*...Nothing...*/
}

Util.getBaseURL = function() {
    //<summary>
    //    Returns the application base URL (well, a URL that is
    //    equivalent to such - it may have some "backtracking", 
    //    i.e. "../" at
    //    the end of the URL to simulate the root...)
    //</summary>    
    // 1) Find the script include for this JavaScript file:
    var scriptElements = document.getElementsByTagName("script"),
        getScriptPathFragmentPosition = Util.getBaseURL._getScriptPathFragmentPosition,
        getScriptPathFragmentPositionResult = getScriptPathFragmentPosition(scriptElements, "js/myscript.js"),
        posScriptPathFragment = getScriptPathFragmentPositionResult.posScriptPathFragment;

    // 2) Create the "base" URL by taking the current URL,
    // stripping the page from the end, and appending
    // sufficient "../" sequences to
    // construct the equivalent of the application's root URL:
    var scriptElementSrcLower = getScriptPathFragmentPositionResult.scriptElementSrcLower;
    var backPathToRoot = ((scriptElementSrcLower !== "") ? scriptElementSrcLower.substring(0, posScriptPathFragment) : "");
    var currentPath = location.href;
    var currentPathWithoutPage;
    var PAGE_TOKEN = ".aspx";
    var posPageToken = currentPath.toLowerCase().indexOf(PAGE_TOKEN);
    if (posPageToken > -1) {
        var trimmedPath = currentPath.substring(0, posPageToken + PAGE_TOKEN.length);
        currentPathWithoutPage = trimmedPath.substring(0, trimmedPath.lastIndexOf("/") + 1);
    } else {
        currentPathWithoutPage = currentPath.substring(0, currentPath.lastIndexOf("/") + 1);
    }
    return (currentPathWithoutPage + backPathToRoot);
};

Util.getBaseURL._getScriptPathFragmentPosition = function(scriptElements, scriptPathFragment) {
    var scriptElementsIndex = (scriptElements.length - 1),
        scriptElementSrc = "",
        scriptElementSrcLower = "",
        posScriptPathFragment = -1;

    while (scriptElementsIndex >= 0) {
        scriptElementSrc = scriptElements[scriptElementsIndex].getAttribute("src");
        if (typeof(scriptElementSrc) != "undefined" && scriptElementSrc !== null && scriptElementSrc !== "") {
            scriptElementSrcLower = scriptElementSrc.toLowerCase();
            posScriptPathFragment = scriptElementSrcLower.indexOf(scriptPathFragment);

            if (posScriptPathFragment >= 0) {
                break;
            }
        }

        scriptElementsIndex--;
    }

    var result = {
        posScriptPathFragment: posScriptPathFragment,
        scriptElementSrcLower: scriptElementSrcLower
    };

    return result;
};

alert(Util.getBaseURL());

SO, on your page you could do:

var myajaxUrl = getBaseURL()+"MyPage/GetDetails";
1

The path you have given is absolutely relative path as long as the hierarchy of your web site remain the same.

The URL you given in ajax call will always move two folder up and call the getDetails method being defined in MyPage

0

try this define <?php $site_root="WWW.example.com/" ?>

var URL="<?php echo $site_root;?>folder/your_action.php";

and trigger Ajax call

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.