2012-05-22 4 views
0

내 jquery ajax 호출에서 상대 경로를 지정하려면 몇 가지 해결책이 필요합니다. 지금은 ../와 같은 것을 사용하고 있습니다. 어떤 제안이라도 고맙게 받아 들여질 것입니다.Jquery Ajax 호출 경로

코드

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

를 트리거 경로는 당신이 당신의 PATH하는 자바 스크립트 파일 – 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()); 

SO, 페이지에 당신은 할 수 :

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

당신이 준 경로는 한 웹 사이트의 계층 구조는 동일하게 유지 절대적으로 상대 경로입니다.

당신은 항상 두 개의 폴더를 이동하고 getDetails의 메소드를 호출 아약스 호출에 주어진 URL이 해킹의 비트입니다

0

이 가을 정의하려고

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

및 Ajax 호출