CORS not working php


CORS not working php

我正试图通过CORS将表单数据从www.siteone.com发布到www.sitetwo.com。我的ajax代码是这样的:

<script>
$(document).ready(function(){
        $("#submit").live('click',function() {
            var url = "http://www.sitetwo.com/cors.php";
            var data = $('#form').serialize();
            jQuery.ajax({
                url : url,
                type: "POST",
                data : $('#form').serialize(),
                }).done(function(response){
                    alert(response);
                    }).fail(function(error){
                    console.log(error.statusText);
                    });
                return false;

});
});
</script>

CCD_ 1中的cors.php文件如下:

<?php
 header('Access-Control-Allow-Origin: *');
 header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
 echo "hai";
?>

但是仍然抛出了Access control Allow Origin错误。抛出的错误是:

XMLHttpRequest cannot load http://www.sitetwo.com/cors.php. Origin http://www.siteone.com is not allowed by Access-Control-Allow-Origin. 

我知道,使用CORS,只允许通过标头访问远程网站,我们就可以使用跨域请求。但当我这样尝试时,就会出现错误。我在这里错过什么了吗?这是我的请求/响应标头:

Response Headers
Connection  Keep-Alive
Content-Length  487
Content-Type    text/html; charset=iso-8859-1
Date    Fri, 23 Aug 2013 05:53:20 GMT
Keep-Alive  timeout=15, max=99
Server  Apache/2.2.15 (CentOS)
WWW-Authenticate    Basic realm="Site two Server - Restricted Area"
Request Headers
Accept  */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length  43
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Host    www.sitetwo.com
Origin  http://www.siteone.com
Referer http://www.siteone.com/index.html
User-Agent  Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:23.0) Gecko/20100101 Firefox/23.0

最后,我自己解决了问题中解释的问题。我为访问标头而实现的代码不正确。

下面提到的两行代码,当给出时,不起作用:

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
?>

但是,正确处理CORS请求需要更多的精力。以下是一个将更充分响应的函数。更新后的代码是:

 <?php
    // Allow from any origin
    if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }
    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         
        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
        exit(0);
    }
    echo "You have CORS!";
?>

我从另一个帖子中找到它奏效了。。。。

允许所有CORS:

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
echo "You have CORS!";

另一个原因是,如果您在php中的任何位置缺少分号或其他内容,即使启用了错误报告,CORS错误消息也将是ajax显示的唯一错误,而您可以通过访问浏览器中的php url来查看实际错误。

请求可能失败的另一个原因是,您的请求指向的URL是否以斜杠结尾。例如,在我的案例中,我不得不以domain.com/api/为目标,因为domain.com/api(无斜杠)无效,并且生成了一个缺少标头的错误。

如果你在浏览器中测试这个URL,它会自动更正,你会认为这个URL是有效的,但请求会失败。非常微妙,很容易忽略结尾的这一斜线。