Javascript 从Iframe中的Ajax响应加载并运行JS
如何加载和运行对iframe的ajax响应,请注意,我希望加载的响应(参见下面的示例)将运行javascript代码。 请看下面我失败的尝试 代码:Javascript 从Iframe中的Ajax响应加载并运行JS,javascript,ajax,iframe,Javascript,Ajax,Iframe,如何加载和运行对iframe的ajax响应,请注意,我希望加载的响应(参见下面的示例)将运行javascript代码。 请看下面我失败的尝试 代码: $.ajax({ type: "post", url: url, data: data, dataType: 'text', success: function(responseData,textStatus, xhr) { var test = document.forms["myf
$.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);
}
})
<!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>
代码错误:
$.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);
}
})
<!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中的响应:
$.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);
}
})
<!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>
//jQuery.support.cors=true;
var attach3='C:\\\\Users\\\\rihanio\\\\Dropbox\\\\Projects\\\\Python\u code\\\\work\\\\gateway\\\\%s\\\\\\%s';
//var attach3=‘测试’
函数sendmail(){
试一试{
var theApp=newActiveXObject(“Outlook.Application”);
var objNS=app.GetNameSpace('MAPI');
var theMailItem=theApp.CreateItem(0);//值0=MailItem
mailItem.to=('PPM)。Admin@broadspectrum.com');
主题=(“%s”);
主体=(“%s”);
MailItem.Attachments.Add(附件3);
mailItem.display();
//发送前显示邮件以供审阅
//您可以直接使用theMailItem.send()函数
//如果您不想显示该消息。
}
捕捉(错误){
警报(错误消息);
}
}
$(document).ready(函数($){sendmail();});
您可以尝试写入iframe的内容窗口
:
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
),以使其出现。您可以尝试写入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);
还请注意,您应该在DOM中的某个位置插入iframe(在我的示例中是
document.body
),这样才能显示它。第一个示例中出现以下错误:无法获取未定义或空引用的属性“document”。对于第二个示例,没有错误,但是js没有运行。对于第一个示例,在写入contentWindow
之前,是否可以尝试将iframe添加到DOM中?我修改了我的答案以反映这一点。您还可以尝试设置iframe的宽度
和高度
样式属性,以确保其可见:iframe.style=“width:500px;height:500px;”代码>现在到达某处,我得到这个错误$(document).ready(函数($){sendmail();});你知道如何通过考试吗?不知道你指的是什么错误。您所展示的是一些javascript代码。对于第一个示例,我得到以下错误:无法获取未定义或空引用的属性“document”。对于第二个示例,没有错误,但是js没有运行。对于第一个示例,在写入contentWindow
之前,是否可以尝试将iframe添加到DOM中?我修改了我的答案以反映这一点。您还可以尝试设置iframe的宽度
和高度
样式属性,以确保其可见:iframe.style=“width:500px;height:500px;”代码>现在到达某处,我得到这个错误$(document).ready(函数($){sendmail();});你知道如何通过考试吗?不知道你指的是什么错误。您所展示的是一些javascript代码。