[Authorize(Roles = "Admin")]
public JsonResult PerformAction()
{
// Your code here
}
$.ajax({
type: "GET",
url: "/Controller/Action",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + token);
},
success: function(data) {
// Your code here
}
});
其中,“Bearer”是身份验证令牌的类型,token是你的身份验证令牌。
以上是一种解决“Ajax调用受保护(需要授权)的控制器方法(MVC)”的具体解法和代码示例。
下一篇:Ajax调用输出和超时