【发布时间】:2013-08-25 07:39:42
【问题描述】:
我正在尝试通过 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>
www.sitetwo.com中的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
【问题讨论】:
-
$_SERVER['HTTP_ORIGIN']有什么价值?你认为它应该有什么价值?注意HTTP_ORIGINisn't mentioned in the documentation for$_SERVER -
我什至试过 header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS');但它没有用。
-
1.) 我不知道您遇到了什么具体问题。你需要澄清这一点。 2.) 与往常一样,对于这些类型的问题,请显示您的请求和响应标头。
-
$=jQuery.noConflict()看起来不对。您告诉 jQuery 将$恢复为原始值,然后将其设置回 jQuery。似乎有点无意义。 -
仍在等待请求/响应标头。
标签: php jquery cross-domain cors access-control