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

The following code works just fine without FriendlyUrls turned on for an ASP.Net Web Forms project:

<script type="text/javascript">
    $(document).ready(function () {

        $.ajax({
            url: '/Default.aspx/GetData',
            type: 'POST',                
            beforeSend: function( xhr ) {
                xhr.setRequestHeader("Content-type",
                     "application/json; charset=utf-8");
            },
            success: function (result) {
                var resultData = (result.d? result.d : result);
                alert(resultData);
            },
            error : function(){
                alert('error');
            }
        });

    });
</script>
Here is the server-side code for the page method (WebMethod):

[System.Web.Services.WebMethod]
public static string GetData()
{                        
    return "Hello";
}
When I load the page in browser, I can see the response as { "d" : "Hello" }, which is the expected result.

Now, if the friendly urls are added using the NuGet package Microsoft.AspNet.FriendlyUrls, the same code would not work. As FriendlyUrls are turned on, I changed the url in jquery ajax call to be "/Default/GetData", but then I would not receive the expected result. Rather I receive the html of the Default.aspx page.

I am struggling to find out why this would not work, the only thing I changed was adding the nuget package for the FriendlyUrls!

I have been trying to find solutions and the most close readable answers I could find were:

Using jQuery for AJAX with ASP.NET Webforms

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Note that all the related posts in given answers do not use FriendlyUrls. I have also seen some answers that indicate that the jquery ajax calls would work fine with MVC, WebAPI, but my project is restricted to use ASP.Net Web Forms.

Am I doing something wrong or missing something here? Has anyone encountered the same scenario for their project? If so, can you please answer how this can be solved? Thanks a bunch for taking time to read and reply
LVL 1
bri_stcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Try to take AJAX out of the equation.

Can you get the service call to work directly in the browser. In other words by typing the URL

/Default/GetData

Into the browser.

Get that working first.
1
MlandaTCommented:
Came across this... you might try that abs see how well it works for you?
Inside ~/App_Start/RouteConfig.cs change:

settings.AutoRedirectMode = RedirectMode.Permanent;
To:

settings.AutoRedirectMode = RedirectMode.Off;
(Or just comment the line)

Also if friendly URLs are enabled you need to change

url: "ConsultaPedidos.aspx/GetClients",
To:

url: '<%= ResolveUrl("ConsultaPedidos.aspx/GetClients") %>',
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.