【问题标题】:Pass iframe text to laravel controller将 iframe 文本传递给 laravel 控制器
【发布时间】:2020-05-12 18:39:17
【问题描述】:

我有问题... 我的 iframe 基本上是一个输入框(下图),我想从用户那里得到输入到我的控制器 我可以通过它传递文本的 html,这是我所期望的,因为它不是一个普通的文本框,我不能将它传递给 laravel,因为它运行 java 代码。 有什么建议吗?

我的页面:

@extends('layouts.app')

@section('content')
    <div class="content">
        <div class="d-flex justify-content-center mt-sm-3">
            <h3>Insert text</h3>
        </div>
        <div class="d-flex justify-content-center mt-xl-1">
            <form method="POST" action="{{ route('test.create') }}">
                @csrf
                <iframe src="http://localhost:8080/test" width="560" height="650"></iframe>

                <button type="submit" class="btn btn-outline-success w-100 mb-3">
                    Done
                </button>
            </form>
        </div>

    </div>

@endsection

图片(仅限 iframe 和按钮):

【问题讨论】:

  • 我猜你需要使用javascript获取iframe中文本框的值。
  • 我尝试在 iframe 上使用带有 id 的 javascript,但我得到 VM404:1 Uncaught DOMException: Blocked a frame with origin "localhost" 无法访问跨域框架。 var iframe = document.getElementById('iframextext'); var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
  • 奇怪,因为我在 jsfiddle 上使用过它并且效果很好
  • 我在控制台上收到错误,当我执行 console.log(iframeDocument);

标签: php laravel iframe


【解决方案1】:

我编写了这段代码,它对我来说运行良好。看看吧:

ex1.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8" />
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <title>Page Title</title>
 <meta name="viewport" content="width=device-width, initial-scale=1">

</head>
<body>
    <form>
        <input type="text" id="sachin"/>
    </form>
</body>
</html>

ex.html

<body>
  <iframe src="ex1.html" id="iframeId" style="height:380px;width:100%"></iframe>
 <script>
  $(function(){
    var iframe = document.getElementById("iframeId");
    $(iframe).on('load',function(){
      var innerDoc = (iframe.contentDocument) 
           ? iframe.contentDocument 
           : iframe.contentWindow.document;
      var input= $(innerDoc).find('#sachin');
      $(input).on('keyup', function(){
          //do anything with the value
          console.log($(this).val());
      });

    });
  });
  </script>
</body>

希望这会有所帮助。

【讨论】:

  • 不,我仍然得到相同的错误 xtext:82 Uncaught DOMException: Blocked a frame with origin "easy.bimby" 访问跨域框架。在 readIframeAndPost (easy.bimby/xtext:82:81) 在 HTMLButtonElement.onclick (easy.bimby/xtext:92:2)
  • 这是什么?无法访问服务器 IP
  • 基本上是 localhost:80 它是本地的,只是在我的 hosts 文件中使用自定义地址,只是 laravel 的东西
  • 基本上我的网站是 localhost:80 而我的 iframe 正在获取 localhost:8080
猜你喜欢
  • 1970-01-01
  • 2013-04-08
  • 1970-01-01
  • 2020-10-06
  • 2018-05-31
  • 2015-01-06
  • 2020-11-30
  • 2019-08-12
  • 2018-12-15
相关资源
最近更新 更多