添加FriendlyUrls时,jQuery ajax调用不适用于ASP.Net Web窗体 [英] jQuery ajax calls not working with ASP.Net Web Forms when FriendlyUrls are added
问题描述
在没有为ASP.Net Web窗体项目打开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 for AJAX与ASP.NET Webforms一起使用
http://encosia.com/使用jQuery直接调用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:
-
将FriednlyUrls添加到项目中.
Add FriednlyUrls to the project.
删除 RegisterRoutes 方法中的行,该行在 App_Start/RouteConfig.cs
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屋!