如何从MVC视图调用ASMX webservice?

时间:2016-04-18 13:38:31

标签: c# ajax asp.net-mvc web-services asp.net-mvc-4

以下是我尝试使用的WebService方法 - :

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

使用ajax调用来调用此方法

$.ajax({
    type: "POST",
    url: "/WebServices/WebService1.asmx/HelloWorld",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    success: function (msg) {
        var data = msg.d;
    },
    error: function (msg) {
        alert(msg.responseText);
    }
});

但每次我收到错误的资源未找到可能是因为我从MVC视图调用它,我也试图把整个localhost路径http://localhost:60005/仍然得到资源未找到的错误,有人可以给我从MVC视图调用asmx webservice的一个小例子?

1 个答案:

答案 0 :(得分:2)

尝试将此行添加到RouteConfig:

routes.IgnoreRoute("{*x}", new { x = @".*\.asmx(/.*)?" });

另外,在你的asmx web服务类中取消注释这一行:

[System.Web.Script.Services.ScriptService]

你有一个很好的教程here