1. 程式人生 > 實用技巧 >ajax 訪問後臺資料出現身份驗證失敗問題的解決

ajax 訪問後臺資料出現身份驗證失敗問題的解決

前端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;即可。

原因還沒深究,有興趣的朋友可以繼續研究。