2

我正在尝试使用 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 功能。

4

2 回答 2

1

文件上传插件使用 iframe 上传文件。由于您无法在没有适当 CORS 标头的情况下跨域发布,并且您无法将 CORS 标头提供给 iframe,因此这根本是不可能的。最好的选择是将其发布到您的服务器,然后根据需要使用服务器端代码将其移动到跨域。

于 2013-01-21T22:45:31.563 回答
0

我知道你不想要任何花哨的东西,但对于没有完全 XHR 支持的浏览器中的跨域,我建议使用jQuery-File-Upload插件。它使用 iframe 将文件上传到另一个域。但它需要更多的工作:

  • 您需要将上传服务器响应更改为插件的预期
  • 您需要在原始服务器中的某处上传result.html以获得响应

如果你不想要 UI 的东西,你可以使用插件的基本版本。

于 2013-01-23T03:36:55.080 回答