我需要一些解决方案来在我的jquery ajax调用中给出相对路径,因为现在我正在使用像../这样的soem。任何建议都会被高度评价。
代码
$.ajax({
type: 'get',
url: '../../MyPage/getDetails',
success: function (data) {
if (data > 0) {
答案 0 :(得分:1)
只要您的网站层次结构保持不变,您提供的路径就是绝对相对路径。
您在ajax调用中给出的URL将始终移动两个文件夹并调用在MyPage中定义的getDetails方法
答案 1 :(得分:1)
这有点像黑客,但你是客户方所以:)
此代码做了一些假设 - 比如myscript.js
的脚本tage存在
假设您的页面上有一些标记,如下所示:\
<script src='../js/myscript.js' type="text/javascript"></script>
<div>hiya</div>
好的,现在我们将使用脚本标记并从中获取基本网址:)
让我们创建一个我们可以使用的Util类,添加一个get base 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";
答案 2 :(得分:0)
试试这个
定义<?php $site_root="WWW.example.com/" ?>
var URL="<?php echo $site_root;?>folder/your_action.php";
并触发Ajax调用