Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CORS HTTP到HTTPS_Php_Ajax_Https_Http Headers_Cors - Fatal编程技术网

Php CORS HTTP到HTTPS

Php CORS HTTP到HTTPS,php,ajax,https,http-headers,cors,Php,Ajax,Https,Http Headers,Cors,我对其他网站的AJAX请求有问题。 当我调用Http到Http时,没有问题。但当我从HTTP呼叫到HTTPS时,出现如下错误消息: 访问位于“”的XMLHttpRequesthttps://www.mywebsideserver.pl/test.php“从 起源'http://mywebsideclient.com'已被CORS策略阻止:否 “Access Control Allow Origin”标头出现在请求的服务器上 资源。php:77 jquery.min.js:2 POST net::

我对其他网站的AJAX请求有问题。 当我调用Http到Http时,没有问题。但当我从HTTP呼叫到HTTPS时,出现如下错误消息:

访问位于“”的XMLHttpRequesthttps://www.mywebsideserver.pl/test.php“从 起源'http://mywebsideclient.com'已被CORS策略阻止:否 “Access Control Allow Origin”标头出现在请求的服务器上 资源。php:77 jquery.min.js:2 POST net::ERR_失败

这些是客户端标题。

这是我的客户端代码:

<script type="text/javascript">

testCORS();
    function testCORS() {
        $.ajax({
          url: 'https://www.mywebsideserver.pl/test.php',
          type: 'POST',
          //dataType: 'html',
          data:{},
         
          crossDomain: true,
          success: function (data, textStatus, xhr) {
            alert(data);
          },
          error: function (xhr, textStatus, errorThrown) {
            console.log(errorThrown);
          }
        });
    }   
</script>

testCORS();
函数testCORS(){
$.ajax({
网址:'https://www.mywebsideserver.pl/test.php',
键入:“POST”,
//数据类型:“html”,
数据:{},
跨域:是的,
成功:函数(数据、文本状态、xhr){
警报(数据);
},
错误:函数(xhr、textStatus、errorshown){
console.log(错误抛出);
}
});
}   
和服务器端简易代码:

<?php
header('Access-Control-Allow-Origin: *');
$myArr = array("xx1", "xx2", "xx3", "xx4");

$myJSON = json_encode($myArr);


echo $myJSON;
?>


需要在htaccess、客户端代码或服务器端代码中添加一些内容?

是的,这就是使用CORS的原因,对安全网站的API请求只能由另一个安全网站完成。因此,使用网站的HTTP无法向HTTPS网站发送请求。你可以阅读更多关于它的内容。HTTPS网站可以对此破例,在其网站上添加一个“Access Control Allow Origin”(访问控制允许来源)标题,表示
“嘿,这个HTTP网站可以访问我的HTTPS网站,因为我是他的朋友,我将破例”。了解更多信息

解决这个问题的一种方法是在发出Ajax请求之前决定使用什么协议(取决于当前的协议)。例如,如果用户要访问,那么您的ajax url应该是

如果用户访问,那么ajax url应该是

您可以按如下方式更改代码:

$is_https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443;

$protocol = $is_https ? 'https' : 'http';
<script type="text/javascript">

testCORS();
    function testCORS() {
        $.ajax({
          url: '<?php echo $protocol ?>://www.mywebsideserver.pl/test.php',
现在,您可以使用
$protocol
变量构建url,如下所示:

$is_https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443;

$protocol = $is_https ? 'https' : 'http';
<script type="text/javascript">

testCORS();
    function testCORS() {
        $.ajax({
          url: '<?php echo $protocol ?>://www.mywebsideserver.pl/test.php',

testCORS();
函数testCORS(){
$.ajax({
url:'://www.mywebsideserver.pl/test.php',

这是否回答了您的问题?