重写URL后,jQuery Ajax不调用webmethod [英] jQuery Ajax not calling webmethod after url rewriting

查看:117
本文介绍了重写URL后,jQuery Ajax不调用webmethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的jQuery AJAX函数未调用我的web方法.这是我的代码:

My jQuery AJAX function is not calling my webmethod. Here is my code:

$.ajax({
    type: "POST",
    url: "Search.aspx/GetCustomers",
    data: '{pageIndex:' + pageIndex + ',searchText:"' + $('#HiddenField1').val() + '",SearchBy:"' + $('#ddlSelectProfile').val() + '"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    failure: function (response) {
        alert(response.d);
    },
    error: function (response) {
        alert(response.d);
    }
});

[WebMethod]
public static string GetCustomers(int pageIndex, string searchText, string SearchBy)
{
    return GetCustomersData(pageIndex,searchText, SearchBy ).GetXml();
}

和在web.config中

and in web.config

<rewrite url="~/Search/(.+)-(.+).html" to="~/Search.aspx?MyTitleId=$1&amp;page=$2" processing="stop" />

推荐答案

在ASP.NET Web表单中,当看起来像用于Web方法的链接(例如,在设置用于删除文件扩展名(例如,删除".aspx")的URL重写规则中,"/WebPageName.aspx/WebMethodName"未获豁免.因此,必须将以下条件添加到URL重写规则中,这看起来像是...

In ASP.NET Web Forms, this challenge arises when links that look like those used for web methods, e.g. "/WebPageName.aspx/WebMethodName", are not exempted in URL rewrite rules set up for removing file extensions (e.g. to remove ".aspx"). So, the following condition must be added to the URL rewrite rule and this can look like...

<rule name="removeAspxExtension" stopProcessing="true">
    <match url="(.*)\.aspx" />
    <conditions>
        <!--This is the condition to be added.-->
        <add input="{URL}" negate="true" pattern="(.*)\.aspx/(.*)" />
    </conditions>
    <action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>

此外,如果RouteConfig.cs中使用了RegisterRoutes(),则必须注释以下几行(以及所有不够具体的行,以至于它们可能影响看起来像"/WebPageName.aspx/WebMethodName"的链接)出来,如下...

Also, if RegisterRoutes() is being used in RouteConfig.cs, the following lines (and all that aren't specific enough such that they could affect links that look like "/WebPageName.aspx/WebMethodName") must be commented out too, as follows...

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // The commented code lines...
    //routes.MapRoute(
    //    name: "Default",
    //    url: "{controller}/{action}/{id}",
    //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    //);
}

现在,当从客户端访问网络方法时,例如通过使用jQuery Ajax,事情应该可以按预期进行.

Now, when a web method is accessed from the client-side, e.g. by using jQuery Ajax, things should work as expected.

这篇关于重写URL后,jQuery Ajax不调用webmethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆