如何获得在Javascript中的相对路径?

编程入门 行业动态 更新时间:2024-10-10 16:21:14
本文介绍了如何获得在Javascript中的相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的ASP web项目,我写了下面的JavaScript code在.js文件中:

In my ASP 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 来表示 mysite/widget/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes ,一切都很好。然而,在一个子目录页面,Javascript的跨$ P $其中pts它的意思mysite/widget/subdirectory/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes而这是行不通的。

When I use this code in the main virtual directory, Javascript interprets Controls/ModelSelectorWebMethods.aspx/getDeviceTypes to mean mysite/widget/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes and all is well. However, from the page in a subdirectory, Javascript interprets it to mean mysite/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中的相对路径?

    本文发布于:2023-05-31 14:02:25,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/393050.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:如何获得   路径   Javascript

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!