我正在尝试使用 jquery 表单插件使跨域文件上传表单工作,但我在 firebug 控制台中不断收到以下错误:
Permission denied to access property 'document'
它发生在 jquery.form 插件的以下行
403: var doc = frame.contentWindow ? frame.contentWindow.document : frame.contentDocument ? frame.contentDocument : frame.document;
如果我使用 safari,我会收到此错误:
Unsafe JavaScript attempt to access frame with URL http://s1.test-host/index.php/upload/start?is-async=1 from frame with URL http://test-host/index.php/main/. Domains, protocols and ports must match.
for 的 html 如下所示:
<form action="http://s1.test-host//index.php/upload/start" method="post" accept-charset="utf-8" id="upload-form" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1204000" />
<label for="file-input">Input File</label>
<input type="file" name="file" value="" id="file-input" alt="Input File" />
<input type="submit" name="upload" value="Convert" id="start-upload" />
</form>
javascript看起来像这样:
$(this).ajaxForm ({
beforeSubmit: onFileFormSubmit, // pre-submit callback
success: onFileFormSubmitEnd, // post-submit callback
resetForm: true, // reset the form after successful submit
dataType: 'json', // force response type to JSON
iframe: true // force the form to be submitted using an iframe
});
目标php页面有这个:
if ($_SERVER['HTTP_ORIGIN']) {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type');
}
... // Usual upload handling code
但似乎没有任何效果。我什至尝试将 'dataType' 更改为 'jsonp' 并将表单方法从 'post' 更改为 'get' 但所有这些都无济于事,我仍然不断收到相同的错误。如果我查看服务器,文件确实会上传,但 javascript 无法调用成功函数。
谷歌搜索后,我发现可能有其他解决方案,如“JQuery 文件上传”或“plupload”,但我真的需要一些简单的东西,我不想使用 HTML5 功能。