最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - React Server Sent Events Unauthorized 401 - Stack Overflow

programmeradmin0浏览0评论

I am using server-sent events in my React project. While it is working on Postman, I am receiving 401 unauthorized on frontend implementation.

useEffect(() => {
const token = localStorage.getItem("authToken");
const evtSource = new EventSource(
  `http://localhost:5001/scienceacademyapi/v1/notification/stream?token=${token}`
);
evtSource.onmessage = (event) => {
  const newNotification = JSON.parse(event.data);
  setShowNotifications((prev) => [...prev, newNotification]);
};

evtSource.onerror = (err) => {
  console.error("Error with sse : ", err);
  evtSource.close();
};

return () => {
  evtSource.close();
};
}, []);
发布评论

评论列表(0)

  1. 暂无评论