简体   繁体   English

未捕获的错误:在 React 中抛出跨源错误

[英]Uncaught Error: A cross-origin error was thrown in React

I am trying to implement a login functionality by following https://www.freecodecamp.org/news/how-to-persist-a-logged-in-user-in-react/ blog.我正在尝试通过关注https://www.freecodecamp.org/news/how-to-persist-a-logged-in-user-in-react/博客来实现登录功能。

I want to persist the user token in local storage.我想将用户令牌保存在本地存储中。

Everything is working fine, but every time I refresh the page, it throws me an error:一切正常,但每次我刷新页面时,它都会抛出一个错误:

Uncaught Error: A cross-origin error was thrown.未捕获的错误:引发了跨源错误。 React doesn't have access to the actual error object in development. React 无法访问开发中的实际错误 object。

I have read to fix it clear the site data here Uncaught Error: A cross-origin error was thrown.我已阅读以修复它并在此处清除站点数据未捕获的错误:引发了跨域错误。 React doesn't have access to the actual error object in development . React doesn't have access to the actual error object in development

But, I want to have a proper solution or any alternate method to bypass it.但是,我想有一个适当的解决方案或任何替代方法来绕过它。

So, that even on the local machine, I can use the login functionality.所以,即使在本地机器上,我也可以使用登录功能。

Here is what I have tried so far:到目前为止,这是我尝试过的:

For checking if the current user is signed in or not:检查当前用户是否登录:

const [userState, setUserState] = useState()

  useEffect(() => {
    const loggedInUser = localStorage.getItem("user");

    if (loggedInUser) {
      const foundUser = JSON.parse(loggedInUser);
      console.log(foundUser);
      setUserState(foundUser);
    }
  }, []);

For login:登录:

const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");

  const handleSubmit = async e => {
    e.preventDefault();

    const user = { username, password };

    const response = await axios.post(
      "http://localhost:8000/api-token-auth/",
      user
    )

    props.setUserState(response.data)
    const loggedInUser = localStorage.getItem("user");

    if (!loggedInUser) {
      localStorage.setItem('user', response.data)
    }
  }

This is a very common problem.这是一个很常见的问题。 It is a security feature to prevent sites from stealing sensitive data from you.这是一项安全功能,可防止网站从您那里窃取敏感数据。 Imagine you loading www.learningjavascript.com , and a malicious attempt is made to myEvilHacker.com without your knowledge, and sending your banking information from when you paid your bills on www.chase.com .想象一下,您正在加载www.learningjavascript.com ,并且在您不知情的情况下对 myEvilHacker.com 进行了恶意尝试,并在您在www.chase.com 上支付账单时发送了您的银行信息。 It's a pain, but necessary.这很痛苦,但很有必要。

The problem is that you are trying to load a resource from another origin;问题是您正在尝试从另一个来源加载资源; for example:例如:

  1. You are on www.myawesomesite.com您在www.myawesomesite.com
  2. You try and load something from www.anothersite.com你尝试从www.anothersite.com加载一些东西

^^^ You are "crossing origins" here. ^^^ 你在这里“穿越起源”。 Sorry Bro, can't do that.对不起,兄弟,不能那样做。

Typically to get around this you must:通常要解决这个问题,您必须:

  1. Allow cross-origin requests on the server-side by enabling CORS.通过启用 CORS 允许服务器端的跨域请求。 Depending on your web server, there there is some setting allow cross-origin requests.根据您的 Web 服务器,有一些设置允许跨域请求。
  2. Move the data onto the same origin.将数据移动到同一原点。 In the above example, put everything on www.myawesomesite.com在上面的例子中,把所有的东西都放在www.myawesomesite.com

Also beware that more recent versions of Chrome will view requests to localhost as cross-origin, and block them.还要注意,较新版本的 Chrome 会将对localhost请求视为跨域请求,并阻止它们。

I had this error message on this snippet from my code:我的代码中的这个片段有这个错误信息:

prevItem.products = JSON.parse(orders[index].orderItems).

But my variable was ALREADY json. After I removed the parse method, everything was fine.但我的变量已经是 json。在我删除解析方法后,一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "<i>Uncaught Error: A cross-origin error was thrown.<\/i>未捕获的错误:引发了跨域错误。<\/b> <i>React doesn&#39;t have access to the actual error object in development<\/i> React 无法访问开发中的实际错误对象<\/b>" - Uncaught Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development 引发了跨域错误。 react 无权访问 react 开发中的实际错误 object - A cross-origin error was thrown. react doesn't have access to the actual error object in development in react React js tsx codesandbox问题 抛出了跨域错误,lodash.isequal - React js tsx codesandbox problem A cross-origin error was thrown and lodash.isequal Codesandbox.io 引发了跨域错误问题 - A cross-origin error was thrown problem with Codesandbox.io × 未处理的拒绝(错误):抛出了跨域错误。 React 无权访问开发中的实际错误对象 - × Unhandled Rejection (Error): A cross-origin error was thrown. React doesn't have access to the actual error object in development getImageData跨源错误 - getImageData cross-origin error 跨域请求错误 - cross-origin request ERROR json.parse 触发 React / MERN 中的跨域错误 - json.parse triggers cross-origin error in React / MERN 更新 React state 变量会引发跨域错误 - Updating a React state variable throws cross-origin error 不确定跨域错误的来源 - Unsure as to Origin of Cross-origin Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM