大家好,这是我的问题,
使用域名,更改我的请求的内容类型。 所以我的问题是,如果我使用的是域名,我就无法发帖。
在客户端,我有这段代码:
JavaScript的:
var myDropzone = new Dropzone('#my-awesome-dropzone', {
url: 'libraries/dropzone/drop.php',
maxFilesize: 3,
addRemoveLinks: removeLinks,
acceptedFiles: 'image/*',
headers: data,
dictFileTooBig : lang.DZFiletobig,
dictFallbackMessage: lang.DZFallback,
dictRemoveFile: lang.DZremoveFile,
dictInvalidFileType: lang.DZinvalidFileType
});
myDropzone.on("addedfile", function (file) {
imgFiles.push(file.name);
}).on("success", function (file, response) {
console.log('Success ' + file.name + ' ' + response);
}).on("error", function (file, errorMessage, xhr) {
swal(lang.Error, file + ' ' + errorMessage, lang.error);
}).on("removedfile", function (file) {
$.post(path.dropzoneRemove, { fileName: file.name });
}).on("sending", function(file, xhr, formData) {
console.log(file, ' ', xhr, ' ', formData);
});
在服务器端,我有这个非常简单的代码段:
PHP:
header('Access-Control-Allow-Origin: *');
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, Authorization, X-Requested-With, accept');
if($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
die("upload failed with error #" . $_FILES['file']['error'] . var_dump(getallheaders()));
}
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetFile = getPath() . $_FILES['file']['name'];
move_uploaded_file($tempFile, $targetFile);
}
我的表单如下:
HTML:
<form method="POST" role="form" enctype="multipart/form-data" class="dropzone" id="my-awesome-dropzone">
<div class="dz-default dz-message">
<h3>Double click or drop files here, to upload pictures</h3>
</div>
</form>
更新 请求域名:
array(9) {
["Connection"] => string(5) "close"
["Content-Type"] => string(33) "application/x-www-form-urlencoded"
["Content-Length"] => string(1) "0"
["X-Ud-Loopcount"] => string(1) "2"
["Cookie"] => string(87) "stuff"
["Referer"] => string(27) "www.example.net"
["Accept"] => string(3) "*/*"
["Host"] => string(23) "example.bplaced.net"
["User-Agent"]=> string(116) "giant-string"
}
无域请求:
array(14) {
["Cookie"] => string(86) "stuff"
["Connection"] => string(5) "close"
["Referer"] => string(35) "http://example.bplaced.net/CMS/"
["Dnt"] => string(1) "1"
["User-Agent"]=> string(116) "giant-string"
["Content-Length"] => string(5) "38292"
["Origin"]=> string(30) "http://example.bplaced.net"
["Content-Type"]=> string(68) "multipart/form-data; boundary=----WebKitFormBoundary7ry6nTLNAXItFBfl"
["Cache-Control"] => string(8) "no-cache"
["Accept-Encoding"] => string(13) "gzip, deflate"
["Accept-Language"]=> string(5) "en-us"
["X-Requested-With"]=> string(14) "XMLHttpRequest"
["Accept"] => string(16) "application/json"
["Host"]=> string(23) "example.bplaced.net"
}