添加 FriendlyUrls 后,jQuery ajax 调用不适用于 ASP.Net Web 表单 [英] jQuery ajax calls not working with ASP.Net Web Forms when FriendlyUrls are added

查看:20
本文介绍了添加 FriendlyUrls 后,jQuery ajax 调用不适用于 ASP.Net Web 表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在没有为 ASP.Net Web Forms 项目打开 FriendlyUrls 的情况下工作得很好:

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>

这是页面方法(WebMethod)的服务器端代码:

Here is the server-side code for the page method (WebMethod):

[System.Web.Services.WebMethod]
public static string GetData()
{                        
    return "Hello";
}

当我在浏览器中加载页面时,我可以看到响应为 { "d" : "Hello" },这是预期的结果.

When I load the page in browser, I can see the response as { "d" : "Hello" }, which is the expected result.

现在,如果使用 NuGet 包添加友好 URL Microsoft.AspNet.FriendlyUrls,同样的代码行不通.由于打开了 FriendlyUrls,我将 jquery ajax 调用中的 url 更改为 "/Default/GetData",但随后我不会收到预期的结果.相反,我收到了 Default.aspx 页面的 html.

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.

我正在努力找出为什么这不起作用,我唯一改变的是为 FriendlyUrls 添加 nuget 包!

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:

将 jQuery 用于 AJAX 和 ASP.NET Webforms

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

请注意,给定答案中的所有相关帖子均未使用 FriendlyUrls.我还看到一些答案表明 jquery ajax 调用可以与 MVC、WebAPI 一起正常工作,但我的项目仅限于使用 ASP.Net Web 表单.

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.

推荐答案

所以,最终我通过对我的项目进行以下更改来解决我的问题:

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

  1. 将 FriednlyUrls 添加到项目中.

  1. Add FriednlyUrls to the project.

删除RegisterRoutes方法中设置App_Start/RouteConfig.cssettings.AutoRedirectMode属性的行强>

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

(请注意,将其设置为 RedirectMode.Permanent 或 RedirectMode.Off 对我不起作用)

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

在web.config中的system.web部分

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

<authorization>
    <allow users="*" />
</authorization>

  • 修改 ajax 调用中的 url 设置为使用 Microsoft.AspNet.FriendlyUrls.Resolve 函数以获得正确的 url,如下所示:

  • 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>
    

  • 这篇关于添加 FriendlyUrls 后,jQuery ajax 调用不适用于 ASP.Net Web 表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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