繁体   English   中英

在iframe中从Ajax响应加载并运行JS

[英]Load and run JS from Ajax response in Iframe

我如何加载并运行对iframe的ajax响应,请注意,我希望加载的响应(请参见以下示例)将运行javascript代码。 请在下面查看我的失败尝试。

码:

$.ajax({
    type: "post",
    url: url,
    data: data,
    dataType: 'text',
    success: function(responseData,textStatus, xhr)
    {
        var test = document.forms["myform"]["checkbox-email"].value;
        if (test == "on" )
        {                               
            console.log(responseData);
            var iframe = document.createElement("iframe");
            iframe.open(); 
            iframe.write(responseData);
            iframe.close();

        }

        else
        {
            //.....                         
        }



    },
    error: function (jqXHR, exception, error) 
    {
         var msg = '';
        if (jqXHR.status === 0) {
            msg = 'Not connect.\n Verify Network.';
        } else if (jqXHR.status == 404) {
            msg = 'Requested page not found. [404]';
        } else if (jqXHR.status == 500) {
            msg = 'Internal Server Error [500].';
        } else if (exception === 'parsererror') {
            msg = 'Requested JSON parse failed.';
        } else if (exception === 'timeout') {
            msg = 'Time out error.';
        } else if (exception === 'abort') {
            msg = 'Ajax request aborted.';
        } else {
            msg = 'Uncaught Error.\n' + jqXHR.responseText;
        }
        console.log(msg);

        $('#status').empty();
        $('#status').show();
         $("#status").attr("class", "error");
        $('#status').html("▼ " + msg).addClass('error');                             
        var div = document.getElementById('status');
        div.innerHTML += " ▼";
        div.innerHTML += jqXHR.responseText;                
        //console.log(jqXHR.responseText);

    } 

})

代码错误:

对象不支持属性或方法“写入”

要在iframe中加载的响应:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8" http-equiv="X-UA-Compatible" content="IE=9" />
<link rel="stylesheet" href="/js/jquery.mobile-1.4.5.min.css">
<script src="/js/jquery-1.11.3.min.js"></script>
<script src="/js/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript">

 //jQuery.support.cors = true; 
 var attach3 = 'C:\\\\Users\\\\rihanio\\\\Dropbox\\\\Projects\\\\Python_code\\\\work\\\\gateway\\\\%s\\\\%s';
 //var attach3 = 'test'
 function sendEmail(){
   try{
      var theApp = new ActiveXObject("Outlook.Application");
      var objNS = theApp.GetNameSpace('MAPI');
      var theMailItem = theApp.CreateItem(0); // value 0 = MailItem

      theMailItem.to = ('PPM.Admin@broadspectrum.com');
      theMailItem.Subject = ('%s');
      theMailItem.Body = ('%s');
      theMailItem.Attachments.Add(attach3);
      theMailItem.display();

      //Show the mail before sending for review purpose
    //You can directly use the theMailItem.send() function
    //if you do not want to show the message.

  }
  catch (err) {
     alert(err.message);
  } 
}

 $(document).ready(function($) {sendEmail(); });
 </script>

您可以尝试写入iframe的contentWindow

var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(responseData);
iframe.contentWindow.document.close();

另外,您可以尝试使用text/html mime类型设置iframe的src属性:

var iframe = document.createElement('iframe');
iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(responseData);
document.body.appendChild(iframe);

还要注意,您应该将iframe插入DOM中的某个位置(在我的示例中为document.body ),以使其出现。

暂无
暂无

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

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