如果我从装有已安装的Nuget包Microsoft.AspNet.FriendlyUrls v 1.0.2和Microsoft.AspNet.Identity v.1.0.0。的jQuery.Ajax调用webmethod,那么我将获得数据对象,但没有data.d但具有Message属性“身份验证失败”。
我的Webmethod.aspx是:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>WebMethod</title> <script src="Scripts/jquery-2.0.3.js"></script> </head> <body> <form id="form1" runat="server"> <h3>Test Webmethod</h3> <div id="greeitng"></div> <div id="innerError" style="border:1px dotted red;display:none;" title="errorMessage"></div> <script type="text/javascript"> function asyncServerCall(username) { jQuery.ajax({ url: 'WebMethod.aspx/HelloWorld', type: "POST", data: "{'username':'" + username + "'}", //async: false, contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { if (data.d == undefined) document.getElementById("greeitng").innerHTML = data.Message; else document.getElementById("greeitng").innerHTML = data.d; }, error: function (err) { if (err.responseText) { $('#innerError').html(err.responseText).show(); } else { alert(err); } } }); } $(document).ready(function () { $('#innerError').hide(); asyncServerCall("Superuser"); }); </script> </form> </body> </html>
我在WebMethod.aspx.cs中的Webmethod是:
[System.Web.Services.WebMethod] public static string HelloWorld(string username) { return string.Format("Hello, {0}", username); }
在Global.asax.cs中激活了路由
void Application_Start(object sender, EventArgs e) { RouteConfig.RegisterRoutes(RouteTable.Routes); }
在App_Start中已重新路由
public static class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { var settings = new FriendlyUrlSettings(); settings.AutoRedirectMode = RedirectMode.Permanent; routes.EnableFriendlyUrls(settings); } }
在您的App_Start文件夹中,在RouteConfig中…。
注释掉以下行或更改其RedirectMode:
//settings.AutoRedirectMode = RedirectMode.Permanent;