jquery ajax calls to asp.net web methods authentication error

I discovered this by creating the application again from scratch thanks to this other site: http://blogs.msdn.com/b/webdev/archive/2013/12/19/building-a-basic-web-forms-application-using-visual-studio-2013.aspx . By following each step carefully and experimenting with the code I realized that when I added the friendly urls framework and enabled Bundling and Minification my jquery ajax call failed. This allowed me ask the correct question to google and I found out that the problem lied in the following files:

1) ~/App_Start/RouteConfig.cs.

    The followint line of code in this file must be changed:

    Fromsettings.AutoRedirectMode =RedirectMode.Permanent;

    Tosettings.AutoRedirectMode = RedirectMode.Off;

 The other alternative is to simply comment it:  

  //settings.AutoRedirectMode =RedirectMode.Permanent;

2) By having friendly URLs enabled, it is necessary to change the way to call the web method, especifically its route:

   From 'userForm.aspx/getAllUsers'

   To'<%= ResolveUrl("userForm.aspx/getAllUsers") %>'

So that the final jQuery code looks like this:

      $.ajax({         url: '<%= ResolveUrl("userForm.aspx/getAllUsers") %>',         async: false,         contentType: 'application/json; charset=utf-8',         success: function (data) {           source.localdata = data.d;         },         error: function (err) {           alert('Error: ' + err);         }       })

With this issue fixed now my app works fine.

posted @ 2014-04-15 14:46  happyu0223  阅读(297)  评论(0编辑  收藏  举报