开发者

relative path to consume local asmx

开发者 https://www.devze.com 2023-03-18 12:19 出处:网络
In my asp.net webapp开发者_如何转开发 I\'m using a local webservice to retrieve data\'s. The system is in a popup (wich is an another page in fact)

In my asp.net webapp开发者_如何转开发 I'm using a local webservice to retrieve data's. The system is in a popup (wich is an another page in fact) So here's the jquery code:

$(document).ready(function () {
    $('#ddlToBind').change(function () {            
        var parameter = "{'aId':'" + $("#ddl").val() + "'}";
        $.ajax({
            url: "../WebServicesASMX/PMywebserv.asmx/Test",
            data: parameter,
            dataType: "json",
            type: "POST",
            contentType: "application/json",
            success: function (data) {
                $('#ddlToBind>option').remove();
                for (var i = 0; i < data.d.length; i++) {

                    $("#ddlToBind").append("<option value='" + data.d[i].Id + "'>" + data.d[i].Name + "</option>");
                };                                       
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });

It is working good while you are in the root of site and once you launch the script in other pages the path is not correct so asmx is not reached..

I've tried with the tilde "~" in place of ../ but it doesn't work.. url: "~/WebServicesASMX/PMywebserv.asmx/Test"

Is it a possibility to specify relatives path in my jquery script?


VirtualPathUtility to the rescue!

url: "<%= VirtualPathUtility.ToAbsolute("~/WebServicesASMX/PMywebserv.asmx/Test") %>",

See this post by Rick Strahl: http://www.west-wind.com/weblog/posts/2009/Dec/21/Making-Sense-of-ASPNET-Paths

Note that for this to work the script needs to be in a page that is processed by ASP.NET e.g. aspx, ascx, MVC View. If your script is in a plain js file, you'll need to move it, or reference a javascript global variable that you define elsewhere in ASP.NET code.


use

<%= ResolveUrl("~/WebServicesASMX/PMywebserv.asmx/Test") %>
0

精彩评论

暂无评论...
验证码 换一张
取 消