Search code examples
javascriptajaxperlsame-origin-policycors

Unable to retrieve the result of a Perl script via Ajax (Same origin policy issue)


So, this Perl script:

http://hacheck.tel.fer.hr/xml.pl

will return a XML result based on the POST form-data that it receives.

I have a web-page on one of my domains (none of which are hacheck.tel.fer.hr) and I would like to use that Perl script via Ajax.

Now, the Same origin policy disallows me to send Ajax requests from my domain like so:

$.post('http://hacheck.tel.fer.hr/xml.pl', {'textarea': '...'}, function(data) {
    // process data
});

The above code throws this error:

XMLHttpRequest cannot load http://hacheck.tel.fer.hr/xml.pl. Origin http://ecmazing.com is not allowed by Access-Control-Allow-Origin.

I would like to know what my options are (I would like to be able to use that Perl script). I know that placing my web-page onto the hacheck.tel.fer.hr domain would obviously solve my issue (and that may in fact be doable, but I'll have to contact the admin for that).

But are there any alternatives?

I've heard about CORS. Could it be used to solve my issue? If I understand correctly, with CORS you have to specify on the server that another domain is permitted, and than web-pages from that other domain can receive responses from your server (or something like that) :)?


Solution

  • I've heard about CORS. Could it be used to solve my issue?

    Yes, but only in browsers that support it. The controller of hacheck.tel.fer.hr would have to set it up.

    But are there any alternatives?

    Proxy the request through your own server