如何获得在Javascript中的相对路径? [英] How to get relative path in Javascript?
问题描述
在我的ASP.net web项目,我写了下面的JavaScript code在.js文件中:
In my ASP.net web project, I've written the following Javascript code in a .js file:
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文件到一个页面中的一个子目录。
It was working great until I tried to load this .js file into a page in a subdirectory.
让我们假设我的项目的名称是小工具
。
Let's suppose that the name of my project is widget
.
当我在主虚拟目录使用code,Java脚本跨$ P $点控制/ ModelSelectorWebMethods.aspx / getDeviceTypes
来表示 https://mysite.com/widget/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes
,一切都很好。然而,在一个子目录页面,Javascript的跨$ P $其中pts它的意思<$c$c>https://mysite.com/widget/subdirectory/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes$c$c>而这是行不通的。
When I use this code in the main virtual directory, Javascript interprets Controls/ModelSelectorWebMethods.aspx/getDeviceTypes
to mean https://mysite.com/widget/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes
and all is well. However, from the page in a subdirectory, Javascript interprets it to mean https://mysite.com/widget/subdirectory/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes
and it doesn't work.
我怎么能写我的JavaScript code,使得AJAX Web方法可以从任何目录网页在我的应用程序调用?
How can I write my Javascript code so that the AJAX web method can be called from pages in any directory in my application?
推荐答案
您已经有了两个选择:
-
建立一个配置/ preferences在JavaScript对象,它包含的所有特定的环境设置:
Build a configuration/ preferences object in JavaScript which contains all your environment specific settings:
var config = {
base: <% /* however the hell you output stuff in ASPX */ %>,
someOtherPref: 4
};
然后preFIX AJAX的URL以 config.base
(和更改值 config.base
无论你是一个开发/测试/部署的服务器上。)
and then prefix the AJAX url with config.base
(and change the value for config.base
whether you're on a dev/ testing/ deployment server.)
使用 &LT;底座/&GT;
HTML标记来设置URL preFIX所有相对URL的。这会影响所有相关网址:形象的,链接等
Use the <base />
HTML tag to set the URL prefix for all relative URL's. This affects all relative URL's: image's, links etc.
就个人而言,我会去选择1.你很可能会发现,配置对象即将派上用场了其他地方。
Personally, I'd go for option 1. You'll most likely find that config object coming in handy elsewhere.
显然,配置对象将被包含在你的网站,服务器副作用code进行评估的一部分;一个的.js
文件将不会削减它没有配置您的服务器。我始终包含在HTML中的配置对象&LT; HEAD&GT;
;它的一个小的配置对象,其内容可以改变每一页上,所以它的完美warrented坚持它在那里。
Obviously the config object will have to be included in a part of your site where server-side-code is evaluated; a .js
file won't cut it without configuring your server. I always include the config object in the HTML <head>
; its a small config object, whose contents can change on each page, so it's perfectly warrented to stick it in there.
这篇关于如何获得在Javascript中的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!