Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 234
uzay95 Avatar asked Dec 31 '25 04:12

uzay95


1 Answers

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")
like image 105
dochoffiday Avatar answered Jan 02 '26 22:01

dochoffiday



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!