Access-Control-Allow-Origin不允许“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();
});
});
感谢。
答案 0 :(得分:0)
浏览器会发送一个转机前请求,通过检查响应标头来检查请求的域是否允许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;
}