通过jquery调用asmx会得到错误的url

时间:2010-10-25 14:43:29

标签: jquery ajax url asmx

几天来,我一直试图让ASMX web服务通过jQuery工作。

继承我的代码

<script type="text/javascript">
    $(document).ready(function () {
        $("#btnTest").click(function () {
            $.ajax({
                type: "POST",
                url: "/WebService.asmx/HelloWorld",
                cache: false,
                contentType: "application/json; charset=utf-8",
                data: "{}",
                dataType: "json",
                success: function (data) {
                    alert(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) { alert(textStatus + ' ' + errorThrown + ' ' + XMLHttpRequest); }
            });
        });
    });
</script>

问题似乎是浏览器检查此网址:

http://localhost:52657/WebService.asmx/CheckSmtp

什么时候应该检查这个网址:

http://localhost:52657/myappname/WebService.asmx/CheckSmtp

所以我在$.ajax网址中没有使用斜杠尝试,但后来情况更糟,因为如果我在页面admin / products / edit上,那么ajax会转到admin/products/edit/webservice.asmx,这肯定是错误的

那么如何告诉jquery查看根文件夹?

问题可能只出在localhost上,但我不能用这种方式测试。

1 个答案:

答案 0 :(得分:1)

这将解析正确的URL服务器端:

<%=ResolveUrl("~/WebService.asmx/HelloWorld")%>