jQuery ajax calls not working with ASP.Net Web Forms when FriendlyUrls are added

我怕爱的太早我们不能终老 提交于 2019-11-30 21:50:36

After spending way too much time on this, I found out that I needed to change this in App_Start/RouteConfig.cs:

//settings.AutoRedirectMode = RedirectMode.Permanent;
settings.AutoRedirectMode = RedirectMode.Off;

So, ultimately I got the solution to my question by making following changes to my project:

  1. Add FriednlyUrls to the project.

  2. Remove the line in RegisterRoutes method that sets settings.AutoRedirectMode property in App_Start/RouteConfig.cs

    (note that setting it to RedirectMode.Permanent or RedirectMode.Off did NOT work for me)

  3. Add authorization in web.config as follows under system.web section

    <authorization>
        <allow users="*" />
    </authorization>
    
  4. Modify the url in ajax call set up to use Microsoft.AspNet.FriendlyUrls.Resolve function in order to get the correct url as below:

    <script type="text/javascript">
       $(document).ready(function () {
           $.ajax({
              url: '<%=Microsoft.AspNet.FriendlyUrls.FriendlyUrl.Resolve("/Default.aspx/GetData")%>',
              type: 'POST',                
              contentType: 'application/json; charset=utf-8',
              dataType: 'json',
              success: function (result) {
                 var resultData = (result.d? result.d : result);
                 alert(resultData);
              },
              error : function(){
                 alert('error');
              }
       });
     });
    </script>
    
Cristian
public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        //Esta wea se deshabilita para que funcione ajax autocomplete.
        //settings.AutoRedirectMode = RedirectMode.Permanent;
        settings.AutoRedirectMode = RedirectMode.Off;
        routes.EnableFriendlyUrls(settings);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!