SCRIPT70:权限被拒绝在IE中访问iFrame

时间:2013-08-06 19:28:39

标签: jquery internet-explorer iframe

我在IE中通过隐藏的iFrame上传文件时遇到了一些问题。实际上文件上传工作正常,但我的jQuery代码显示成功对话框并添加到表的链接没有触发。使用IE开发人员工具,我发现了SCRIPT70:Permission denied错误消息。这在Chrome中运行良好,所以我对IE中的问题感到茫然。我应该提到我正在使用IE10,所以我想在IE的早期版本中也存在这个问题。

基本上我要做的是使用隐藏的iFrame模拟像文件上传一样的Ajax,因为我们必须支持旧版浏览器。当iFrame成功发布后,它的响应有一个div,其中包含我读过的JSON然后解析。来自JSON的数据用于向用户显示指示文件上载状态的消息,并通过向表添加行来将链接添加到页面。但是在IE中,chechUploadResponse函数似乎甚至没有被触发。

使用Javascript:

$(document).ready(function()
{
$('#btnPrint').click(openPrintTimesheetWindow);
$('#date').change(postback);
$('#employee').change(postback);
$('#client').change(postback);
$('#btnUpload').click(uploadFile);
$("#uploadFrame").on("load", function () {
    $('#uploadFrame').contents().find('#userFile').change(uploadFileChanged);
    checkUploadResponse();
    });
});

function postback()
{
$('#timesheetPrintFilter').submit();
}

function uploadFileChanged()
{
$('#ajaxBusy').show();
    $('#uploadFrame').contents().find('#uploadForm').submit();
}

function uploadFile()
{
    var employeeId  = $('#init_employee').val();
    var periodDate  = $('#init_periodEndDate').val();

    $('#uploadFrame').contents().find('#employeeId').val(employeeId);
    $('#uploadFrame').contents().find('#periodEndDate').val(periodDate);
    $('#uploadFrame').contents().find('#userFile').click();
}

function checkUploadResponse()
{
    var response = $('#uploadFrame').contents().find('#uploadResponse').text();

    if (response != null && response != '')
    {
        var response = jQuery.parseJSON(response);

        if (response.status == "ERROR")
        {
            $("#dialog").html(response.message);
            $("#dialog").dialog({ buttons: { "OK": function() { $(this).dialog("close");}}, title: "Error" });
        }
        else
        {
            $("#dialog").html(response.message);
            $("#dialog").dialog({ buttons: { "OK": function() { $(this).dialog("close");}}, title: "Success" });

            var url = response.url;
            var tsaid = response.tsaid;
            var name = response.name;

            var row = '<tr id="tsaid-' + tsaid + '">' +
                    '<td width="80%" valign="top" align="left">' +
                        '<a href="' + url + '">' + name + '</a>' +
                    '</td>' +
                '</tr>';

            $("#tsAttachment").append(row);
        }
    }

    $('#ajaxBusy').hide();
}

隐藏的iFrame:

<form id="uploadForm" name="uploadForm" action="timesheet-upload.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="3145728" />
    <input type="hidden" name="employeeId" id="employeeId" value="" />
    <input type="hidden" name="periodEndDate" id="periodEndDate" value="" />
    <input type="file" name="userFile" id="userFile" />
</form>

以下是发布后隐藏的iFrame的示例响应

<div id="uploadResponse">{"status":"SUCCESS","message":"Timesheet successfully uploaded","url":"uploads\/2013\/Aug\/1-49cd1c0217abf676505b349ec88bb5a42b1d5631e41232f08be3b0dced9f65e2.pdf","name":"How To Write A Cover Letter.pdf","tsaid":15}</div>
<form id="uploadForm" name="uploadForm" action="timesheet-upload.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="3145728" />
    <input type="hidden" name="employeeId" id="employeeId" value="" />
    <input type="hidden" name="periodEndDate" id="periodEndDate" value="" />
    <input type="file" name="userFile" id="userFile" />
</form>

1 个答案:

答案 0 :(得分:9)

我知道这篇文章有点陈旧,但这可能有助于未来寻求者了解IE的神秘面纱。

建议的解决方案需要应用于 jQuery库 。 问题在这里解释: https://connect.microsoft.com/IE/feedback/details/802251/script70-permission-denied-error-when-trying-to-access-old-document-from-reloaded-iframe

并在此处给出了解决方案:

https://github.com/jquery/sizzle/blob/5b3048605655285a81b06fbe4f49f2a14a8d790f/src/sizzle.js#L472-L480

另一个解决方案位于jQuery错误报告网站的此故障单下:http://bugs.jquery.com/ticket/14535 它由用户 muley 发布,并且还提供了JSFiddle:http://jsfiddle.net/xqb4s/

在这种情况下需要在jQuery库中添加的代码是:

// MY EDIT - this try/catch seems to fix IE 'permission denied' errors as described here:
// http://bugs.jquery.com/ticket/14535
try{
    document === document; //may cause permission denied
}
catch(err){
    document = window.document; //resets document, and no more permission denied errors.
} 

在:

function Sizzle( selector, context, results, seed )
相关问题