【问题标题】:Remote Upload (using blueimp file upload plugin)远程上传(使用 blueimp 文件上传插件)
【发布时间】:2014-05-15 18:45:59
【问题描述】:

远程上传:通过给定的 URL 上传文件。

我想使用blueimp 插件向我的网站添加远程上传。 我找到了这个链接:https://gist.github.com/blueimp/5075976,但是我使用外部服务器上传我的文件,所以当我编写这段代码时,我得到了错误:

Uncaught SecurityError: Blocked a frame with origin "http://myExternalURL.com" from accessing a frame with origin "http://www.myMainWebsite.com". Protocols, domains, and ports must match. 

带有url的部分代码:

$('#remote-file-copy').on('submit', function (e) {
    e.preventDefault();
    var url = $(this).find('input').val(),
        iframe = $('<iframe src="javascript:false;" style="display:none;"></iframe>');
    if (url) {
        iframe
            .prop('src', 'http://I-USE-EXTERNAL-URL/remote-file-copy.php?url=' + encodeURIComponent(url))
            .appendTo(document.body);
    }
});

我该如何解决?

非常感谢! 对不起我的英语

【问题讨论】:

    标签: php jquery file-upload same-origin-policy blueimp


    【解决方案1】:

    remote-file-copy.php中的&lt;?php之后添加这一行

    header("Access-Control-Allow-Origin: *");
    

    这将告诉浏览器允许来自其他来源的 javascript 访问您的 php 文件中的内容,如果您只想允许来自您的站点,您也可以这样做:

    header("Access-Control-Allow-Origin: http://yourmaindomain.com");
    

    更多阅读: MDN ,enable-cors.org,another question

    另请参阅:chrome-blocks-different-origin-requests

    【讨论】:

    • 您给出的错误消息与跨域来源有关,我给您的代码应该适用于从域 A 到域 B 的 ajax 调用,但如果是 iframe,请查看 iframe cross domain javascript calls,似乎喜欢一篇好文章给你一些线索。
    • @ArielAharonson 顺便说一句,代码生成错误是 iframe 中的 Javascript,(可能包含window.parent)不是您发布的代码,如果您在我链接到的文章中使用了 document.domain 技巧,并且它仍然无法发布该代码。如果您无法按照 madskristensen 的博客中的建议创建子域,您或许应该使用 php proxy 或根本不使用 iframe(也许将所有 js 代码放在主域中并使用 jsonp 检查进度?)。
    • @ArielAharonson 你能发布你的解决方案吗?我也有同样的问题。谢谢:)
    • @AkoSiAsong 看答案。您需要在远程服务器上添加Access-Control-Allow-Origin 标头
    猜你喜欢
    • 2013-02-13
    • 2016-07-25
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多