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

时间:2023-01-28 18:29:02

For a few days now I have been trying to get an ASMX webservice to work via jQuery.

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

Heres my code

继承我的代码

<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>

The problem seems to be that the browser checks for this url:

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

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

When it should really check for this url:

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

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

So I tried without the slash in the $.ajax url, but then it's worse, because if I'm on a page admin/products/edit then the ajax goes to admin/products/edit/webservice.asmx which is definitely wrong.

所以我尝试没有$ .ajax网址中的斜线,但后来更糟糕,因为如果我在一个页面admin / products / edit然后ajax转到admin / products / edit / webservice.asmx这绝对是错误的。

So how do I tell jquery to look in the root folder?

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

It may be that the problem is only on localhost, but I can't test anything this way.

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

1 个解决方案

#1


1  

This will resolve the right url server side:

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

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

#1


1  

This will resolve the right url server side:

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

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