2013-08-06 46 views
5

我在IE中通過隱藏的iFrame上傳文件時遇到了一些問題。其實文件上傳工作正常,但我的jQuery代碼顯示成功對話框並添加鏈接到表不會觸發。使用IE開發人員工具,我發現SCRIPT70:Permission denied錯誤消息。這在Chrome中運行良好,所以我對在IE中遇到的問題不知所措。我應該提到我使用的是IE10,所以我想這個問題也存在於以前的IE版本中。SCRIPT70:權限被拒絕在IE中訪問iFrame

基本上我想要做的就是使用隱藏的iFrame來模擬Ajax文件上傳,因爲我們必須支持傳統瀏覽器。當iFrame發佈成功時,其響應中包含一個包含我讀取然後解析的JSON的div。來自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> 
+0

事實證明,這可能是一個jQuery錯誤。我從jQuery-1.9.1升級到jQuery-1.10.2,這似乎解決了我的問題。我發現這個報告在jQuery網站上有興趣的人。希望這有助於任何有類似問題的人。 http://bugs.jquery.com/ticket/13936 – greyfox

回答

8

我知道這個帖子是有點老了之後的示例響應,但這可能有助於未來的尋求者解決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) 
+0

這解決了我的問題,並保存了我的一天!我的情況是,一旦加載內部文件,iframe中的頁面調用外部文檔的基於jQuery的函數,然後外部文檔的jquery不再工作,只是拋出'access is denied'錯誤。 –

+0

'文檔===文檔'行將被JS uglify/minify工具刪除,任何其他方式來觸發權限拒絕?謝謝。 –

+0

我正在使用jquery 2.2.4;找不到解決方案或找到這樣的代碼。請爲此版本提供。 –