简体   繁体   English

Codesandbox.io 引发了跨域错误问题

[英]A cross-origin error was thrown problem with Codesandbox.io

I have "A cross-origin error was thrown" Error in Codesanbox.io when i tried to execute this code.当我尝试执行此代码时,Codesanbox.io 中出现“抛出跨域错误”错误。 please what can i do to go over ?请问我该怎么办? I'm using Create-react-app with codesanbox.io我在 codesanbox.io 中使用 Create-react-app

import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";

class App extends React.Component {
  state = {
    customer: [
      { id: 1, name: "Leon" },
      { id: 1, name: "Lea" },
      { id: 1, name: "Vanelle" }
    ]
  };
  render() {
    const title = "Customer list";
    const elements = this.state.customer.map();
    return (
      <div className="App">
        <h1>{title}</h1>
        <ul>
          <li>
            Philippe <button>X</button>
          </li>
        </ul>
        <form>
          <input type="text" placeholder="Add a customer" />
          <button>Confirm</button>
        </form>
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Your render function should map through each of the customer found in the state.您的render函数应该map到状态中找到的每个customer Just calling this.state.customer.map() throws an error which it seems is not handled properly by codesandbox .只是调用this.state.customer.map()会抛出一个错误,它似乎没有被codesandbox正确处理。

Try this for your render:试试这个为你的渲染:

render() {
    const title = "Customer list";
    return (
      <div className="App">
        <h1>{title}</h1>

        <ul>
        {
          this.state.customer.map(c => (
            <li key={c.id}>
             {c.name} <button>X</button>
            </li>
          ))
        }
        </ul>
        <form>
          <input type="text" placeholder="Add a customer" />
          <button>Confirm</button>
        </form>
      </div>
    );
  }

May be it's codesandbox's fault?可能是codeandbox的错? Did you read here: https://github.com/codesandbox/codesandbox-client/issues/667你在这里读过吗: https : //github.com/codesandbox/codesandbox-client/issues/667

Normally, cross-origin error is something you can control in the backend instead of frontend (browser).通常,跨源错误是您可以在后端而不是前端(浏览器)中控制的。 I suspect that you have called some API in the code that you shouldn't have the permission to call in the browser.我怀疑你在代码中调用了一些你不应该有权在浏览器中调用的 API。 CORS is a whole topic to read. CORS 是一个完整的阅读主题。 To truly understand, I think you should spend some time to digest it.要真正理解,我认为你应该花一些时间来消化它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM