【问题标题】:Access-Control-Allow-Origin Not Working on Apache访问控制允许来源在 Apache 上不起作用
【发布时间】:2016-03-07 20:45:44
【问题描述】:

当我从本地主机向服务器发送 ajax 帖子时,我不断遇到跨源问题:

OPTIONS http://domain:port/fileuploader/fileupload No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. plupload.full.min.js:14
XMLHttpRequest cannot load http://domain:port/fileuploader/fileupload. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. 

我正在使用 apache,所以我添加了这个以允许一切,但它现在仍在工作:

 Header add Access-Control-Allow-Origin *
 Header add Access-Control-Allow-Headers "SOAPAction, Authorization, username, password, origin, X-Requested-With, Content-Type, XMLHttpRequest"
 Header add Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"
 Header add Access-Control-Allow-Credentials: "true"

我浏览器的响应头是:

Request URL:http://domain:port/fileuploader/fileupload
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,ar;q=0.6
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:domain:port
Origin:http://localhost
Pragma:no-cache
Referer:http://localhost/omnixuploader/index.html?dDocName=test8818&dDocTitle=test8818&dDocType=SiebelAttachment&dDocAuthor=SADMIN&lan=ara&files=test|jpg:png:txt|220,wwow|pdf:txt:zip|222&rand=19
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Response Headersview source
Allow:GET, HEAD, POST, TRACE, OPTIONS
Content-Length:0
Date:Thu, 09 Jan 2014 14:39:45 GMT
X-ORACLE-DMS-ECID:a3121f70a766128a:-422d6665:14370f7c3a3:-8000-000000000000057a
X-Powered-By:Servlet/2.5 JSP/2.1

我不明白为什么它不起作用?有什么提示吗?

【问题讨论】:

    标签: apache cors same-origin-policy


    【解决方案1】:

    您似乎正在配置 http://localhost 以允许其他网站告诉浏览器向它发出请求。

    您需要告诉http://domain:port/ 这样做。

    X-Powered-By:Servlet/2.5 JSP/2.1 表明您可能正在使用 Apache Tomcat。描述了如何配置该服务器in answers to this question

    【讨论】:

    • 我正在尝试从 localhost 向我的域发布帖子,因此上面的配置是在域 Apache httpd.conf 上完成的,并且域具有(标题添加 Access-Control-Allow-Origin *)
    • 那么看起来您正在配置在端口 80 上运行的服务器,而不是在您的示例中用 :port 替换的端口号上运行的服务器。
    • 我发的帖子是7001,我该怎么办?
    • 配置运行在 7001 端口的服务器,而不是运行在 80 端口的服务器。
    【解决方案2】:

    尝试先设置标题,然后添加其他标题:

    Header set Access-Control-Allow-Origin *
    
    Header add Access-Control-Allow-Headers "SOAPAction, Authorization, username, password, origin, X-Requested-With, Content-Type, XMLHttpRequest"
    
    Header add Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"
    
    Header add Access-Control-Allow-Credentials: "true"
    

    【讨论】:

      猜你喜欢
      • 2012-03-16
      • 1970-01-01
      • 2016-04-04
      • 2016-11-10
      • 2015-02-17
      • 2016-05-07
      • 2012-04-27
      • 2019-12-12
      • 1970-01-01
      相关资源
      最近更新 更多