Dynamically getting base url inside js file

I want to run a function inside a 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 my root url will be ( http: // localhost: 4399 / VirDir or something else it may be) the address inside the js file, And I need to find the root folder of the application to find the 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 a website with C #.

any help would be greatly appreciated

+3
source share
3 answers

Maybe I missed something, but if 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>

, HTTP javascript, , , URL- .

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;
    }
0

-, :

HEAD :

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

    function ResolveUrl(url) {

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

        return url;
    }

</script>

.cs:

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

javascript:

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

.

<script> , src script (, <script src="my/js/dir/myScript.js"></script>), .

, , "myScript.js" , .

- - , , - :

: app-config.js


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

.

0
source

Source: https://habr.com/ru/post/1739013/


All Articles