[英]How to get relative path in Javascript?
在我的ASP.net Web項目中,我在.js文件中編寫了以下Javascript代碼:
function getDeviceTypes() {
var deviceTypes;
$.ajax({
async: false,
type: "POST",
url: "Controls/ModelSelectorWebMethods.aspx/getDeviceTypes",
data: '{ }',
contentType: "application/json;",
dataType: "json",
success: function(response) {
deviceTypes = response.d;
},
error: function(xhr, status) {
debugger;
alert('Error getting device types.');
}
}); // end - $.ajax
return deviceTypes;
}
在我嘗試將此.js文件加載到子目錄中的頁面之前,它工作得很好。
假設我的項目名稱是widget
。
當我在主虛擬目錄中使用此代碼時,Javascript將Controls/ModelSelectorWebMethods.aspx/getDeviceTypes
解釋為https://mysite.com/widget/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes
,一切都很好。 但是,從子目錄中的頁面,Javascript將其解釋為https://mysite.com/widget/subdirectory/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes
,它不起作用。
如何編寫我的Javascript代碼,以便可以從我的應用程序中任何目錄中的頁面調用AJAX Web方法?
你有兩個選擇:
在JavaScript中構建配置/首選項對象,其中包含所有特定於環境的設置:
var config = { base: <% /* however the hell you output stuff in ASPX */ %>, someOtherPref: 4 };
然后前綴AJAX的URL與config.base
(和更改值config.base
無論你是一個開發/測試/部署服務器上。)
使用<base />
HTML標記為所有相對URL設置URL前綴。 這會影響所有相對URL:圖像,鏈接等。
就個人而言,我會選擇選項1.您很可能會發現配置對象在其他地方派上用場。
顯然,配置對象必須包含在您的站點中評估服務器端代碼的部分; 如果不配置服務器, .js
文件將不會刪除它。 我總是在HTML <head>
包含配置對象; 它是一個小的配置對象,其內容可以在每個頁面上進行更改,因此完全可以將其粘貼在那里。
只要你不關心asp.net 虛擬目錄 (這實際上不可能從腳本中找出來,你必須從服務器傳遞一些東西),你可以查看URL並解析它:
function baseUrl() {
var href = window.location.href.split('/');
return href[0]+'//'+href[2]+'/';
}
然后:
...
url: baseUrl()+"Controls/ModelSelectorWebMethods.aspx/getDeviceTypes",
...
...現在我從上面的評論中看到虛擬目錄是個問題。 我通常這樣做。
1)在你的母版頁中,放置代碼以在某處注入腳本,最好在其他任何東西之前(我通過添加控件而不是使用ScriptManager將它直接添加到HEAD)以確保它在任何其他腳本之前運行。 C#:
string basePath = Request.ApplicationPath;
// Annoyingly, Request.ApplicationPath is inconsistent about trailing slash
// (if not root path, then there is no trailing slash) so add one to ensure
// consistency if needed
string myLocation = "basePath='" + basePath + basePath=="/"?"":"/" + "';";
// now emit myLocation as script however you want, ideally in head
2)更改baseUrl以包括:
function baseUrl() {
var href = window.location.href.split('/');
return href[0]+'//'+href[2]+basePath;
}
創建應用程序根變量...
var root = location.protocol + "//" + location.host;
在進行AJAX請求時使用絕對URI(而不是相對)...
url: root + "/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes"
我認為這個函數可以工作......它是一個相對路徑為“../../../”所以如果你在每個頁面中調用這個函數,這將返回一個相對路徑格式。
function getPath() {
var path = "";
nodes = window.location. pathname. split('/');
for (var index = 0; index < nodes.length - 3; index++) {
path += "../";
}
return path;
}
您可以在開頭導入名稱空間:System.Web.Hosting.HostingEnvironment
<%@ Master Language="VB" AutoEventWireup="false" CodeFile="Site.master.vb" Inherits="Site" %> <%@ Import namespace="System.Web.Hosting.HostingEnvironment" %>
並在js上:
<script type="text/javascript"> var virtualpathh = "<%=ApplicationVirtualPath %>"; </script>
你能用window.location.pathname
嗎?
var pathname = window.location.pathname;
$.ajax({
//...
url: pathname + 'Controls/...', // might need a leading '/'
//...
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.