【问题标题】:Origin not allowed by Access-Control-Allow-Origin PerlAccess-Control-Allow-Origin Perl 不允许来源
【发布时间】:2013-01-08 13:57:52
【问题描述】:

我正在向远程 perl 服务器发出请求。但问题是

XMLHttpRequest cannot load http://otherdomain.com/getPub.pl?content=hello. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

我已经在 perl 脚本中启用 access_control_allow_origin 为“*”,代码如下:

#!/usr/bin/perl

use strict;
use CGI qw(:standard);
use warnings;

my $cgi = new CGI;

print $cgi -> header(
-type => 'text/plain',
-access_control_allow_origin => '*',
  );
my $content = $cgi -> param('content');
open(CON,">content.txt") || die "can't open $!";
print CON $content;
close(CON);

js请求如下:

function sendData(){
    var url = "http://otherdomain.com/getPub.pl?content=hello";
    var xhr = createCORSRequest("GET", url);
    if(!xhr){
        throw new Error ('CORS not supported');
        }
    xhr.send();

}
function createCORSRequest(method, url){
     var xhr = new XMLHttpRequest();
     if("withCredentials" in xhr){
       xhr.open(method,url,true);
     }else if(typeof XDomainRequest != "undefined"){
        xhr = new XDomainRequest();
        xhr.open(method, url);
    }else{
            xhr = null;
    }
return xhr;
} 

响应头为:

Allow:GET,HEAD,POST,OPTIONS,TRACE
Connection:Keep-Alive
Content-Length:0
Content-Type:text/plain; charset=UTF-8
Date:Mon, 07 Jan 2013 16:55:44 GMT
Keep-Alive:timeout=15, max=99
Server:Apache/2.2.3 (CentOS)

怎么了?

【问题讨论】:

  • 查看浏览器开发者工具中的网络标签。检查正在发出哪些请求。反应是你所期望的吗?您是否看到意外的 OPTIONS 请求? (您可能会被预检检查绊倒)。
  • 通过检查浏览器控制台或curlwget等工具中的完整标头来检查Perl是否真的返回了您想要的标头。
  • 嗨@Quentin,这确实是由于 OPTIONS 请求。我不知道如何在 perl 中响应它。
  • @Quentin 嗨,它终于可以使用 PHP,如下所示。我真的不知道原因。

标签: perl cors


【解决方案1】:

它终于可以在 PHP 中使用了,尽管我仍然没有看到区别。 总结为:

1。 使用 perl 时:

my $cgi = new CGI;
print $cgi -> header(
-type => 'text/plain',
-access_control_allow_origin => '*',
-access_control_allow_headers => 'content-type,X-Requested-With',
-access_control_allow_methods => 'GET,POST,OPTIONS',
-access_control_allow_credentials => 'true',
); 

HTTP 标头为:

请求头:

Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:origin, x-requested-with, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:example.org
Origin:http://localhost
Referer:http://localhost/testCORS.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko)         Chrome/23.0.1271.97 Safari/537.11

响应头:

Allow:GET,HEAD,POST,OPTIONS,TRACE
Connection:Keep-Alive
Content-Length:0
Content-Type:text/plain; charset=UTF-8
Date:Tue, 08 Jan 2013 05:52:26 GMT
Keep-Alive:timeout=15, max=100
Server:Apache/2.2.3 (CentOS)

2。 但是在 PHP 中,它可以工作!!!:我没有看到差异!

代码为:

<?php
 header("Access-Control-Allow-Origin: *");
 header("Access-Control-Allow-Methods: GET,POST,OPTIONS");
 header("Access-Control-Allow-Headers: X-Requested-With,");
 #header("Access-Control-Allow-Credentials: true");
 ?>

响应头:

Access-Control-Allow-Headers:X-Requested-With
Access-Control-Allow-Methods:GET,POST,OPTIONS
Access-Control-Allow-Origin:*
Connection:Keep-Alive
Content-Length:0
Content-Type:text/html; charset=UTF-8
Date:Tue, 08 Jan 2013 05:52:10 GMT
Keep-Alive:timeout=15, max=100
Server:Apache/2.2.3 (CentOS)
X-Powered-By:PHP/5.3.3

【讨论】:

    【解决方案2】:

    您需要确保您的服务器使用正确的 Access-Control-Allow-Origin 标头响应对该 URL 的 OPTIONS 请求。浏览器将通过首先发出 OPTIONS 请求来“预检”您的请求。如果失败,它根本不会尝试您的请求。

    【讨论】:

    • 嗨,Charles,是的,它要求 OPTIONS。但是,我不知道如何响应 OPTIONS 请求。谢谢
    • 对其他请求的响应相同,但只有标头,没有正文。您应该能够配置您的 Web 服务器以在收到此类请求时运行 Perl 脚本。除了设置标题之外,脚本本身不需要做任何事情。
    • 对 OPTIONS 请求的响应也应该包含 Access-Control-Allow-Methods 和 Access-Control-Allow-Credentials 标头。一个只处理 OPTIONS 请求的 CGI 脚本只有一些打印语句,甚至不需要使用任何 Perl 模块。所以你可以用if ($ENV{REQUEST_METHOD} eq 'OPTIONS') { print the three header lines and a blank line } else { do regular script for GET method }之类的东西开始你的脚本
    • 感谢您的回答。我仍然找不到 perl 中的错误。但是它可以在 PHP 中使用,如下所示。
    猜你喜欢
    • 2013-02-05
    • 2011-10-27
    • 2021-10-09
    • 2013-04-09
    相关资源
    最近更新 更多