【发布时间】:2017-05-02 21:29:05
【问题描述】:
我正在尝试制作一个简单的 php 后端来处理另一台服务器中的联系表单,但是尽管添加了正确的标头,它仍然给我同样的错误消息:
XMLHttpRequest cannot load https://php-contact-form-lual.herokuapp.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4000' is therefore not allowed access. The response had HTTP status code 404.
这是ajax请求:
$.ajax({
type: 'POST',
url: 'https://php-contact-form-lual.herokuapp.com/',
data: {
subject: 'subject',
to: 'receiver',
name: $('#name').val(),
email: $('#email').val(),
msg: $('#msg').val()
}
}) // then the callbacks
这是php:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
// return only the headers and not the content
// only allow CORS if we're doing a POST - i.e. no saving for now.
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'POST') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
}
exit;
}
// handling the data
$subject = $_POST['subject'];
$to = $_POST['to'];
$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['msg'];
$msg = "DE: " . $name . " (" . $email .")" . "\n\n" . $msg;
mail($to, $subject, $msg);
?>
请注意,“处理数据”块之前的代码行取自 this answer,我还尝试使用相同答案的第一部分中提供的更简单的解决方案 - 在其他地方也找到 - 甚至替换带有特定 URL 的星号,但结果是相同的 :(
任何帮助将不胜感激:)
更新:我在服务器端尝试过的事情的日志(从最旧到当前):
// 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);
}
------------------------------------------
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, OPTIONS");
-----------------------------------------
header("Access-Control-Allow-Origin: http://localhost:4000");
header("Access-Control-Allow-Methods: POST, OPTIONS");
-----------------------------------------
header("Access-Control-Allow-Origin: http://localhost:4000");
header("Access-Control-Allow-Methods: POST, OPTIONS, GET");
-----------------------------------------
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'POST') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers');
}
exit;
}
------------------------------------------
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'POST') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers');
}
exit;
}
// + sending headers though ajax
------------------------------------------
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers');
-------------------------------------------
# created .htaccess file with this line:
Header set Access-Control-Allow-Origin "*"
------------------------------------------
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS, GET');
header('Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers');
---------------------------------------------
header('Access-Control-Allow-Origin: http://localhost:4000');
header('Access-Control-Allow-Methods: POST, OPTIONS, GET');
header('Access-Control-Allow-Headers: X-Requested-With, content-type, access-control-allow-origin, access-control-allow-methods, access-control-allow-headers');
-----------------------------------------------
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
// return only the headers and not the content
// only allow CORS if we're doing a POST - i.e. no saving for now.
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'POST') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
}
exit;
}
--------------------------------------------------
header('Origin: http://localhost:4000');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
附加信息
请求标头
POST / HTTP/1.1
Host: php-contact-form-lual.herokuapp.com
Connection: keep-alive
Content-Length: 88
Accept: */*
Origin: http://localhost:4000
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:4000/contacto/
Accept-Encoding: gzip, deflate, br
Accept-Language: es,en-GB;q=0.8,en;q=0.6,de;q=0.4
响应标头
HTTP/1.1 404 Not Found
Connection: keep-alive
Date: Sat, 17 Dec 2016 16:10:02 GMT
Server: Apache
Content-Length: 198
Content-Type: text/html; charset=iso-8859-1
Via: 1.1 vegur
【问题讨论】:
-
这是一个重复的查询。尝试从这里获得帮助stackoverflow.com/questions/20035101/…
-
谢谢@Nadeem,但我已经遇到了这个问题,php 的答案不起作用,而且大多数其他答案都没有直接解决问题,而是提供第三方解决方案或解决方案不基于 php 的后端。我真的很想为 php 解决它作为智力兴趣,但是我强烈考虑用其他语言重写后端。
-
奇怪的是响应标头根本没有提及您的访问标头。 heroku 是否过滤您可以返回的标题?
-
我看到服务器返回一个
404错误;您确定在https://php-contact-form-lual.herokuapp.com/index.php下的index.php文件中有上面的PHP 代码吗?你真的需要https吗?服务器是否也接受单个http请求,如果是,为什么不尝试在没有 SSL 的情况下使用它?另外,您是否尝试使用 jQuery$.ajaxdataType: "jsonp"和JSON.stringify({})将数据作为 JSON 数据传递给$.ajaxdata的对象? -
@ChristosLytras 你是完全正确的,我在消息的第一部分得到了如此固定(没有 cors 标题......),我认为 404 是标题而不是其他方式的结果大约。读完这篇文章后,我去了目录,发现 index.php 名称中有一个错字。请正式回答,以便我接受您的回答(@Robbie 也答对了,但您比他早 3 小时发表评论),非常感谢