views:

96

answers:

4

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

A: 

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 (e.g. <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.

Pablo Cabrera
You are right, but i am working on my machine with this base url : http://localhost:4399/myVirDir/... but when i publish this site to the server its base address is changing with http://www.hostname.com/theirVirDir
uzay95
+1  A: 

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")
Doc Hoffiday
A: 

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;
    }
Mike_G
A: 

in your index file[ or equivalent ],set

P.M