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