How to use Access-Control-Allow-Origin: https://www.example.com?

一笑奈何 提交于 2019-12-21 04:01:22

问题


I want to make HTTPS calls from an HTTP webpage. I hope to solve this problem with Access-Control-Allow-Origin. How can I use it?


回答1:


On the HTTPS page (that you are requesting from the HTTP page) set the header:

Access-Control-Allow-Origin: http://www.example.com

You can do this in PHP with:

<?php
    header("Access-Control-Allow-Origin: http://www.requesting-page.com");
?>

Alternatively if that doesn't work, you could create a file on your HTTP server (where the request is coming from) that downloads and displays the contents, this can be done in PHP with:

<?php
    echo  file_get_contents("https://www.requested-page.com");
?>

I would not advise doing this as it requires extra bandwidth and isn't good practice, it should only be used if you can't do the first option. Furthermore, if a developer has set the access control to be restricted it's probably for a reason.




回答2:


You can't, really. It's the browser preventing it. Security reasons. You can look into cURL. Read this posting: https-request-via-ajax-from-http-page



来源:https://stackoverflow.com/questions/13857631/how-to-use-access-control-allow-origin-https-www-example-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!