更新时间:2022-05-08 21:28:31
使用AJAX解决,
错误是我从 js 文件中获取API,无法到达我的"Cookies"并返回
The Error was my fetch API from js file, couldn't reach my "Cookies" and return
options.AccessDeniedPath ="/account/accessdenied";
在
services.ConfigureApplicationCookie
.
当我调试 notification.js 响应
对象是
response = Response {type: "cors", url: "{mysite}.com/account/login?ReturnUrl=%2Fapi%2Fnotifications", redirected: true, status: 200, ok: true, …}
如果将您的 Cookie 设置为 HttpOnly = true ,那么您将使它们无法从 client 辅助脚本访问.
If you set your Cookies to HttpOnly=true then you are making them unreachable from client side scripts.
因此,我在 .script
标签中的 .cshtml
下直接进行了Ajax调用,而我的Ajax调用是:
So, I've made an Ajax call directly under my .cshtml
in a script
tag and my Ajax call was:
function notifyUser() {
var ul = document.getElementById("notification-list");
ul.innerHTML = "";
var notificationDropdownBtn = document.getElementById("notification-dropdown");
var initialNotificationLimit = 2;
notificationDropdownBtn.onclick = () => {
notifyUser();
}
$.ajax({
type: "GET",
//In here I directly use my controller and action.
url: "@Url.Action("Notifications", "Portal")",
contentType: "application/json",
dataType: "json",
success: function (notifications) {
var unreadNotifications = notifications.filter(notification => !notification.isRead);
if (unreadNotifications.length > 0) {
...
} else {
...
},
error: function (req, status, error) {
console.error(error);
}
});
}