Lua*_*ual 9 php ajax jquery heroku cors
我正在尝试制作一个简单的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.
Run Code Online (Sandbox Code Playgroud)
这是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
Run Code Online (Sandbox Code Playgroud)
这是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);
?>
Run Code Online (Sandbox Code Playgroud)
请注意,"处理数据"块之前的代码行是从这个答案中获取的,我也尝试使用同一答案的第一部分中提供的更简单的解决方案 - 也在其他地方找到 - 甚至用特定的替换星号网址,但结果是一样的:(
任何帮助,将不胜感激 :)
更新:我在服务器端试过的东西的日志(从最旧到最新):
// 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');
Run Code Online (Sandbox Code Playgroud)
有条件的信息
请求标头
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
Run Code Online (Sandbox Code Playgroud)
响应标头
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
Run Code Online (Sandbox Code Playgroud)
我看到服务器返回404错误.这表明您在文件中没有上面的PHP代码.index.phphttps://php-contact-form-lual.herokuapp.com/index.php
另外,考虑一下你是否真的需要https.服务器是否也接受单个http请求,如果是,为什么不尝试在没有SSL的情况下使用它?
最后,您是否尝试使用jQuery $.ajax dataType: "jsonp"和JSON.stringify({})一个对象将数据作为JSON数据传递$.ajax data?
| 归档时间: |
|
| 查看次数: |
2067 次 |
| 最近记录: |