简体   繁体   中英

dynamically getting base url inside js file

I want to run function inside web service (.asmx file)

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: '/Admin/WebSrvcs/Management.asmx/f_SearchLabel',
        data: "{_sQuery:'" + obj.value + "'}",
        dataType: "json",

But I don't know where will be my root url( http://localhost:4399/VirDir or something else it may be) address inside js file. And i need to reach root folder of application to find asmx file.

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: 'http://localhost:4399/virDir/Admin/WebSrvcs/Management.asmx/f_SearchLabel',
        data: "{_sQuery:'" + obj.value + "'}",
        dataType: "json",

I am working on Visual Studio 2008 and building web site with C#.

any help would be greatly appreciated

If you're using Master Pages then this becomes handy:

In the HEAD of Master Page:

<script type="text/javascript">
    var baseUrl = '<%# ResolveUrl("~/") %>';

    function ResolveUrl(url) {

        if (url.indexOf("~/") == 0) {
            url = baseUrl + url.substring(2);
        }

        return url;
    }

</script>

In the Master Page .cs page:

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Header.DataBind();
    }

Then in your javascript:

ResolveUrl("~/Admin/WebSrvcs/Management.asmx/f_SearchLabel")

There are some ways you could do this.

One would be to iterate over the <script> elements on the page and check if the src attribute contains the desired script name (eg <script src="my/js/dir/myScript.js"></script> ) and then extract the path you want.

Although this could be an easy way to port things across other servers, you could have the problem that " myScript.js " could be included more than once in different locations, so it wouldn't be that much reliable.

Another way to do this would be to include some sort of configuration file where you could set up your application settings, using something like this:

File: app-config.js


var AppConfig = {
    "someImportantPath" : "some/important/path",
    "anotherPath" : "another/path"
}

And the you would use this global variable across your application.

Maybe Im missing something, but if the javascript and the page are on the same server you can just use js to do something like this:

<script>
var pd = parent.document;

var location = pd.location.protocol + "//" + pd.location.host;

alert(location);
</script>

Also, you could write an HTTP handler for your javascript, and when the request comes in, you could fill in a variable by getting the full url of the request.

internal static string GetFullPath(HttpRequest request)
    {
        Uri uri = request.Url;
        string fullUrl = String.Format("{0}{1}{2}{3}", (request.IsSecureConnection) ? "https://" : "http://",
                                       uri.Host, (uri.IsDefaultPort) ? "" : String.Format(":{0}", uri.Port), uri.AbsolutePath);
        Int32 index = fullUrl.LastIndexOf("/");
        fullUrl = fullUrl.Remove(index + 1, (fullUrl.Length - 1) - index);

        return fullUrl;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM