doushantun0614 2015-06-19 15:36
浏览 51
已采纳

使用JQuery / javascript绕过跨源策略而无法访问远程服务器

I want to retrieve the following JSON using javascript:

https://www.gov.uk/api/organisations/skills-funding-agency

If I do a JsonP call I get:

callback=jQuery1113043552865949459374_1434727982051&_=1434727982052' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled.

If I do a JSON call I get:

XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

Is this at all possible? I've been googling for hours without any success. I've seen some PHP solutions but ideally don't want to have to install PHP to do this.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • doudi8829 2015-06-19 15:40
    关注

    If you don't wanna install PHP to do this, why did you tag with php? You need to use a Server Side Script like Proxy PHP file, that reads the content and executes it correctly.

    Proxy.php:

    <?php
        header("Content-type: application/json");
        die(file_get_contents($_GET["url"]));
    ?>
    

    And call it like this:

    url: "proxy.php?url=http://gov.uk/blah/blah"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?