前端JS:
function getAjaxData() { var strResult = ""; $.ajax({ type: 'post', url: encodeURI(‘Manage.aspx/GetConfigUrl’), data: "{}", contentType: "application/json;charset=utf-8", dataType: "json", cache: false, async: false, success: function () { return data; }, error: function (err) { if (err.responseText !== null && err.responseText !== '') { alert(err.responseText); } } }); return strResult; }
后台:普通asp.NET项目代码类,AJAX访问的函数接口,需要增加 [WebMethod],并且函数接口为static静态函数。
using System.Web.Services; public partial class Manage : Page
{
private static string strServerUrl = WebConfigurationManager.AppSettings["serverurl"]; [WebMethod] public static string GetConfigUrl() { return strServerUrl; }
}
执行时出错,提示:Message:身份验证失败。
经过百度,在一个国外网站找到解决办法:
将项目中的RouteConfig.cs文件打开,注释掉:settings.AutoRedirectMode = RedirectMode.Permanent;即可。
原因还没深究,有兴趣的朋友可以继续研究。