-1

Remote upload: Upload files via a given URL.

I want to add a remote upload to my website using blueimp plugin. I found this link:https://gist.github.com/blueimp/5075976, but I use an external server to upload my files, so when I write this code, I get the error:

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. 

The part of the code with the 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);
    }
});

How can I fix it?

Thank you very much! and sorry for my English

HTMHell
  • 4,572
  • 4
  • 30
  • 70

1 Answers1

1

add this line right after <?php in remote-file-copy.php

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

this will tell the browser to allow javascript from other origins to access content from your php file , if you only want to allow from your site you can also do this:

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

more reading on this: MDN ,enable-cors.org, another question

see also : chrome-blocks-different-origin-requests

Community
  • 1
  • 1
Bor691
  • 597
  • 1
  • 11
  • 20
  • the error message you gave is related to cross domain origin , the code i gave you should work for ajax calls from domain A to domain B but in case of iframe take a look at [iframe cross domain javascript calls](http://madskristensen.net/post/iframe-cross-domain-javascript-calls) , seems like a good article to give you some clues . – Bor691 Apr 08 '14 at 09:47
  • @ArielAharonson btw the code generating error is Javascript inside iframe , (perhaps one that includes `window.parent`) not the code you posted , if you did the document.domain trick in the article i linked to and it still doesn't works post that code . if you can't make a subdomain as suggested in madskristensen's blog you should perhaps use a [php proxy](https://github.com/softius/php-cross-domain-proxy) or don't use an iframe at all (perhaps put all js code in maindomain and check progress with jsonp?). – Bor691 Apr 08 '14 at 09:54
  • @ArielAharonson can you post your solution? I'm also having the same problem. Thanks :) – eaponz Mar 03 '15 at 08:06
  • @AkoSiAsiong Look at the answer. You need to add the `Access-Control-Allow-Origin` header on the remote server – HTMHell Mar 03 '15 at 19:48