我如何加载并运行对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);
}
})
代码错误:
对象不支持属性或方法'write'
要加载到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>
答案 0 :(得分:2)
您可以尝试写入iframe的contentWindow
:
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(responseData);
iframe.contentWindow.document.close();
或者,您可以尝试使用src
mime类型设置iframe的text/html
属性:
var iframe = document.createElement('iframe');
iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(responseData);
document.body.appendChild(iframe);
另请注意,您应该在DOM(我的示例中为document.body
)的某处插入iframe,以便显示。