获取“Access-Control-Allow-Origin不允许原始http://domain.com。”错误。我正在尝试做的是我有一个时事通讯系统,我在一个子域和另一个子域设置时事通讯。当用户输入不正确的电子邮件并提交信息时,代码运行良好,但是它不会显示“成功,电子邮件发送的消息”,并且我在提交时收到错误。
$('#submitButton').click(function(e) {
var ap = $('.appear-first:eq(0)').fadeOut(200);
if ($('#email').val() === '' || $('#email').val().search('@') == -1){
ap.css('color','#f00').text('Please enter a valid Email Address.').fadeIn(300);
e.preventDefault();
} else {
var a = $('#subscribeform');
console.log(a);
$.post('http://domain.domain.com/?p=subscribe&id=1', a.serialize(), function(re) {
console.log(re);
ap.css('color','#fff').text('Thank you for subscribing to our newsletter. You will be emailed shortly to confirm your address.').fadeIn(300);
});
}
e.preventDefault();
});
});谢谢。
发布于 2013-06-04 00:26:01
浏览器将发送飞行前请求,通过检查响应头来检查请求的域名是否允许CORS。
您请求的域应该知道您请求的域。
所以在domain.com上实现如下代码(PHP示例):
$allowed_domains = array("http://sub.domain.com");
if (isset($_SERVER['HTTP_ORIGIN'])
&& in_array($allowed_domains, $_SERVER['HTTP_ORIGIN'])) {
// emit CORS header
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
// enable session/cookies
header('Access-Control-Allow-Credentials: true');
// optional headers here
header("Access-Control-Allow-Headers: Content-Type");
}
if ($_SERVER["REQUEST_METHOD"] === "OPTIONS") {
// CORS pre-flight request, we don't need to do anything else
exit;
}https://stackoverflow.com/questions/16901272
复制相似问题