PHP 标头不适用于 Access-Control-Allow-Origin
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18468961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP header not working for Access-Control-Allow-Origin
提问by Caleb
I am using the jQuery File Upload plugin by Blueimpto upload images to a server. The problem is, the sending server is admin.example.com
, and the receiving server where the images are stored is on www.example.com
. Same domain, different subdomain.
我正在使用Blueimp的jQuery File Upload 插件将图像上传到服务器。问题是,发送服务器是admin.example.com
,而存储图像的接收服务器是www.example.com
。同一个域,不同的子域。
I followed the instructions here on setting up cross-domain uploads, and everything seems to be correct as far as code, but when I try to upload the images, I get this error:
我按照此处设置跨域上传的说明进行操作,就代码而言,一切似乎都是正确的,但是当我尝试上传图像时,出现此错误:
XMLHttpRequest cannot load http://www.example.com/upload/. Origin http://admin.example.com is not allowed by Access-Control-Allow-Origin.
The upload folder does have read and write permissions.
上传文件夹确实具有读写权限。
I'm going to post my code below-if anyone can show me how to fix this, please let me know. I had asked about this before and was going to try some other solutions (iframe uploads and ftp file moving). Neither of these will be best for my situation, and it would be easiest if I could just do it this way...
我将在下面发布我的代码 - 如果有人可以告诉我如何解决这个问题,请告诉我。我之前问过这个问题,打算尝试其他一些解决方案(iframe 上传和 ftp 文件移动)。这些都不适合我的情况,如果我可以这样做,那将是最简单的......
RECEIVING SERVER
接收服务器
index.php
<?php
header('Access-Control-Allow-Origin: http://admin.example.com'); //I have also tried the * wildcard and get the same response
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
?>
<?php
error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');
$upload_handler = new UploadHandler();
SENDING SERVER
发送服务器
main.js
$(function () {
'use strict';
// Initialize the jQuery File Upload widget:
$('#fileupload').fileupload({
// Uncomment the following to send cross-domain cookies:
xhrFields: {withCredentials: true},
url: 'http://admin.example.com/upload/',
disableImageResize: false,
dropZone: $('#dropzone'),
imageMaxWidth: 1800,
imageMaxHeight: 1800,
});
});
Again I've tried the iframe file upload, so please don't suggest it unless you can give me full working code...
我再次尝试了 iframe 文件上传,所以请不要建议它,除非你能给我完整的工作代码......
I have also tried header('Access-Control-Allow-Origin: *');
but get the same error...I'm trying to get this finished by the weekend so I'd appreciate any help I can get. :)
我也尝试过,header('Access-Control-Allow-Origin: *');
但遇到了同样的错误......我正在努力在周末完成这项工作,所以我很感激我能得到的任何帮助。:)
Thanks!
谢谢!
EDIT: here's the response headers for the failed OPTIONS request
编辑:这是失败的 OPTIONS 请求的响应头
Allow:OPTIONS, TRACE, GET, HEAD, POST
Content-Length:0
Date:Tue, 27 Aug 2013 15:08:29 GMT
Public:OPTIONS, TRACE, GET, HEAD, POST
Server:Microsoft-IIS/7.5
X-Powered-By:ASP.NET
回答by Aitem
I use this headers and its work for me
我使用这个标题及其对我的工作
header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
回答by Hong
I would try to set this on the web.config since you're operating on an IIS server:
我会尝试在 web.config 上设置它,因为您在 IIS 服务器上运行:
https://gist.github.com/corydeppen/3518666
https://gist.github.com/corydeppen/3518666
<system.webServer>
<httpProtocol>
<customHeaders>
<!-- allowing all-->
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
回答by bob
The solution for me was to re-save the PHP file using UTF-8 encoding. Apparently the original text file (that I copied into place and overwrote for the quick test) used was using some other funky encoding.
我的解决方案是使用 UTF-8 编码重新保存 PHP 文件。显然,使用的原始文本文件(我复制到位并覆盖以进行快速测试)使用的是其他一些时髦的编码。