MAO-EYE 2016-09-04 09:45 采纳率: 100%
浏览 30

AJAX发布HTML代码

I'm having an issue with sending some HTML code using AJAX please see my code below

<iframe src="http://www.w3schools.com" width="10" height="10" id="awc_frame"></iframe>
<script>var iframe = document.getElementById("awc_frame");</script>

Here is the AJAX code below

<script> 

    $.ajax({
        type: "POST",
        url: "mobileView.php",
        data: { val : iframe },
        success: function(data){
            console.log(data); 
        }
    })
</script>

The code isn't sending the variable to the PHP file. Looking into the Network side of things it sends text ie if I put "" around iframe it sends this code "val = iframe" but not the actual code within the iframe. The "var iframe"does work and pulls back the HTML code of the iframe

Please tell me what I'm doing wrongly.

Thanks in advance.

EDIT: I'm sorry. It's not the HTML code within the iFrame I need to send, It's the entire iFrame code I need to send.

Another Edit: What I'm trying to accomplish when a visitor from my company goes to my website I would like Javascript or Jquery to load an internal website from the visitors computer and then have all of the code from that website that's on the client's end to be sent to a Server which will store the entire iFrame code in a database.

  • 写回答

4条回答 默认 最新

  • weixin_33694620 2016-09-04 09:51
    关注

    what are you trying to do?

    var iframe = document.getElementById("awc_frame");
    

    above code is an javascript object of your iframe which contains a lot of properties.. since you are using jQuery, you could get that with:

    var iframe = $('#awc_frame');
    

    keep in mind that above code is the element it self in jquery object format you could get element object like this:

    var iframe = $('#awc_frame')[0];
    

    ** you're doing something wrong.

    if you're trying to get iframe HTML content:

    var iframe_contents = $("#awc_frame").contents();
    

    if you explain more about what you are trying to do, i can update my answer to suit you.

    * UPDATE * considering what you are trying to do..

    Method #1: (Easy Way)

    you could use php to fetch content of the website you need:

    <?php
        $contents = file_get_contents('http://www.w3schools.com');
        // Saving $contents to database...
    ?>
    

    Method #2: (Hard Way)

    as @mdziekon said, you first should wait until your iframe gets loaded then:

    var iframe = $("#awc_frame");
    
    iframe.on("load", function () {
        var contents = $(this)[0].innerHTML;
    
        $.ajax({
            type: "POST",
            url: "mobileView.php",
            data: {
                val: contents
            },
            success: function(data){
                console.log(data);
            }
        });
    });
    

    hope it solves your problem

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向