2012-05-22 57 views
0

我需要一些解決方案,以便在我的jquery ajax調用中給出相對路徑,至此我正在使用soem類似於../的東西。任何建議都會受到高度關注。Jquery Ajax調用路徑

代碼

$.ajax({ 
     type: 'get', 
     url: '../../MyPage/getDetails', 
     success: function (data) { 
      if (data > 0) { 
+2

的路徑應該是相對於你在瀏覽器中看到不JavaScript文件 – Imdad

+0

確定的路徑URL不帶任何擴展與'getDetails'即結束? – Imdad

+0

Imdad getDetails是一個控制器方法 – Kurkula

回答

1

這是一個黑客位被定義的,但你是客戶端等等:)

此代碼做了一些假設 - 像對myscript.js腳本踏歌存在

假設你有你的頁面上的一些標記,像這樣:\

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

OK,現在我們將使用腳本標記並從:)

得到基本URL讓我們創建我們可以使用一個實用程序類,添加一個get基本URL方法,並提醒該值:

工作在這裏複製: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()); 

所以,你的頁面上,你可以這樣做:

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

你給出的路徑是絕對的相對路徑,只要你的網站的層次結構保持不變。

您在Ajax調用給出總會提出兩項文件夾中並調用getDetails方法的URL在我的頁面

0

試試這個 定義<?php $site_root="WWW.example.com/" ?>

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

,並觸發Ajax調用