Javascript 如何使用 Access-Control-Allow-Origin:https://www.example.com?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13857631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 14:57:31  来源:igfitidea点击:

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

javascriptajaxjquerycors

提问by user1874941

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?

我想从 HTTP 网页进行 HTTPS 调用。我希望用Access-Control-Allow-Origin. 我怎样才能使用它?

回答by Rob Farr

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

在 HTTPS 页面(您从 HTTP 页面请求)设置标头:

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

访问控制允许来源:http: //www.example.com

You can do this in PHP with:

您可以在 PHP 中使用以下命令执行此操作:

<?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:

或者,如果这不起作用,您可以在您的 HTTP 服务器(请求来自)上创建一个文件来下载并显示内容,这可以在 PHP 中完成:

<?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.

我不建议这样做,因为它需要额外的带宽并且不是好的做法,只有在您无法执行第一个选项时才应该使用它。此外,如果开发人员将访问控制设置为受限,这可能是有原因的。

回答by bobthyasian

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

你不能,真的。这是浏览器阻止它。安全原因。您可以查看 cURL。阅读这篇文章:https-request-via-ajax-from-http-page