【问题标题】:How to check the status code of URL in iFrame in React?如何在 React 中检查 iFrame 中 URL 的状态码?
【发布时间】:2022-01-13 14:29:06
【问题描述】:

我在 react 组件中使用 iframe。我想检查 url 是否已响应 200 并完全加载,并想处理错误和加载场景。我试图访问 iframe id,但它抛出了 corss 原始帧错误。请推荐

【问题讨论】:

    标签: reactjs iframe cross-domain


    【解决方案1】:

    如果您想在 iframe 完全加载之前添加加载,您可以通过检查 iframe 的文档何时加载 document.getElementById('myframe').contentWindow.document 来处理它 你可以在你的 react 组件中创建一个函数并像这样检查 iframe:

      const [ loading, setLoading ] = useState(true);
      const checkIframe = () => {
        const iframeDocument = document.getElementById('myframe').contentWindow.document;
        if(iframeDocument){
            setLoading(false);
        } else{
          setTimeout(() => {
            checkIframe();
          }, 1000);
        }
      }
    
    

    【讨论】:

    • 但是通过尝试访问myframe,它给出了cross orgin frame 错误。
    • 实际上,如果您使用 javascript 访问 iframe,这是一个安全问题:stackoverflow.com/a/25098153/6606524
    猜你喜欢
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 2015-04-26
    • 2013-10-03
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 2017-12-18
    相关资源
    最近更新 更多