1

我正在尝试将来自 ajax 请求的传入 URL 映射到 Global.asax 中的 Web 服务方法。Web 服务位于此路径/ajax/BookWebService.asmx中,它有 2 个方法GetListOfBookGetListOfAuthor.

我想在 Script 标签中使用 url:/ajax/book/list而不是 url: 。/ajax/BookWebService.asmx/GetListOfBook

这是我的脚本和标记:

<script type="text/javascript">

  $(function () {
      $("#btnCall").click(function () {

          $.ajax({type: "POST",
              url: "/ajax/book/list",
              data: "{}",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
              success: function (response) {
                  var list = response.d;
                      $('#callResult').html(list);
              },
              error: function (msg) {
                  alert("Oops, something went wrong");
              }
          });

      });
  });

</script>

<div>
  <div id="callResult"></div>
  <input id="btnCall" type="button" value="Call Ajax" />
</div>

这是 Global.asax :

void Application_BeginRequest(object sender, EventArgs e)
{
    var currentRequest = HttpContext.Current.Request.Url.AbsolutePath;

    if (currentRequest.Contains("ajax/book/list")) {
        HttpContext.Current.RewritePath("/ajax/BookWebService.asmx/GetListOfBook");
    }

}

当我检查 FireBug 控制台时,这就是我得到的:

“NetworkError:500 内部服务器错误 - localhost:13750/ajax/book/list”

如果我将url:/ajax/book/list更改为url:/ajax/BookWebService.asmx/GetListOfBook它会按预期工作。

我正在使用 VS 2010 和 .NET 4。

我如何做 ajax 调用/ajax/book/list而不是/ajax/BookWebService.asmx/GetListOfBook

4

1 回答 1

2

如果您可以将 ajax 调用更改为类型:“GET”,请尝试以下更改:

阿贾克斯调用:

    $(function () {
        $("#btnCall").click(function () {

            $.ajax({ type: "GET",  //Change 1
                url: "/ajax/book/list",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var list = response.d;
                    $('#callResult').html(list);
                },
                error: function (msg) {
                    alert("Oops, something went wrong");
                }
            });

        });
    });

</script>

全球.asax:

    void Application_BeginRequest(object sender, EventArgs e)
    {
        var currentRequest = HttpContext.Current.Request.Url.AbsolutePath;
        //Change 2
        if (currentRequest.Contains("ajax/book/")) // maybe you can use a generic path to all rewrites...
        {
            IgnoreWebServiceCall(HttpContext.Current);
            return;
        }
    }
    //Change 3
    private void IgnoreWebServiceCall(HttpContext context)
    {
        string svcPath = "/ajax/BookWebService.asmx";

        var currentRequest = HttpContext.Current.Request.Url.AbsolutePath;

        //You can use a switch or....
        if (currentRequest.Contains("ajax/book/list"))
        {
            svcPath += "/list";
        }

        int dotasmx = svcPath.IndexOf(".asmx");
        string path = svcPath.Substring(0, dotasmx + 5);
        string pathInfo = svcPath.Substring(dotasmx + 5);
        context.RewritePath(path, pathInfo, context.Request.Url.Query);
    }

BookWebService.asmx:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService] ///******************* Important
    public class BookWebService : System.Web.Services.WebService
    {

        [WebMethod]

        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] ///******************* Important
        public string list()
        {
          return "Hi";

        }

     }

为我工作:)

于 2012-10-02T00:54:10.780 回答