Origin不允许由Perl控制

pu3pd22g  于 2023-10-24  发布在  Perl
关注(0)|答案(3)|浏览(154)

我正在向远程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)

什么事?

4ngedf3f

4ngedf3f1#

它终于在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 Header为:
请求标头:

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
enxuqcxy

enxuqcxy2#

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

oyt4ldly

oyt4ldly3#

你可以在apache上使用.htaccess文件设置它,把它放在你的perl文件的同一个目录下,这就解决了我的问题。

<Files "getPub.pl">
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
    Header set Access-Control-Allow-Headers "Content-Type"
</Files>

相关问题