在这里我从 Jquery Ajax 调用 web 方法。在成功函数中我看到有一个名为“身份验证失败”的错误
这里附上错误图片
我的网络方法
[WebMethod,ScriptMethod]
public static List<UploadedFiles> GetAllUploadedFiles()
{
List<UploadedFiles> UploadedFilesDetails = new List<UploadedFiles>();
try
{
SqlCommand comGetAllFiles = new SqlCommand("SP_GetAllUploadedFiles", conDB);
comGetAllFiles.CommandType = CommandType.StoredProcedure;
if (conDB.State == ConnectionState.Closed)
conDB.Open();
SqlDataReader rdr = comGetAllFiles.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(rdr);
foreach (DataRow r in dt.Rows)
{
UploadedFilesDetails.Add(new UploadedFiles
{
Id = (int)r["Id"],
UserId =(Guid)r["UserId"],
FilePath = r["FilePath"].ToString(),
Date =(DateTime) r["Date"]
});
}
}
catch(Exception ee)
{
}
finally
{
conDB.Close();
}
return UploadedFilesDetails;
}
我的 Ajax 函数
<script>
$(function () {
LoadUploadFiles();
});
function LoadUploadFiles() {
var url = '<%=ResolveUrl("WebMethods.aspx/GetAllUploadedFiles") %>';
$.ajax({
url: url,
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (Result) {
debugger;
$.each(Result.d, function (key, value) {
alert("y");
$("#uploaddata").append($("<table><tr></tr></table>").val
(value.Id).html(value.FilePath));
});
},
error: function (e, x) {
alert(x.ResponseText);
}
});
}
</script>
在我的 Web.Config 文件中
<location path="WebMethods.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
最佳答案
我找到了答案
只需在 RouteConfig 文件中的行下面注释即可
//settings.AutoRedirectMode = RedirectMode.Permanent;
关于c# - 从 jquery AJAX 调用 web 方法时身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23014866/