我想使用plupload庫將文件上傳到遠程服務器。一切適用於Chrome(32.0)和IE 10使用html5運行時,但當我嘗試使用Firefox 27(html5運行時)或IE 8(html4運行時)時,出現錯誤Error #-200: HTTP Error.
。Plupload跨域上傳200 http錯誤
客戶端腳本:
$(function() {
var uploader = new plupload.Uploader({
browse_button: 'browse',
url: 'https://remote.com/API/action.php',
runtimes : 'html5,flash,silverlight,html4',
flash_swf_url : './js/Moxie.swf',
silverlight_xap_url : './js/Moxie.xap'
});
uploader.init();
uploader.settings.multipart_params = {
[...]
};
// PreInit events, bound before any internal events
uploader.bind('init', function(up, info) {
console.log('[Init]', 'Info:', info, 'Features:', up.features);
alert(info['runtime']);
});
uploader.bind('Error', function(up, err) {
document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
});
document.getElementById('start-upload').onclick = function() {
uploader.start();
};
});
與Chrome的第一個請求:
Request URL:https://remote.com/API/action.php
Request Method:OPTIONS
Status Code:200 OK
與Chrome的第二個請求:
Request URL:https://remote.com/API/action.php
Request Method:POST
Status Code:200 OK
請求頭
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:hipt.ucc.ie
Origin:http://server.com
Pragma:no-cache
Referer: XXX
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
響應頭
Access-Control-Allow-Headers:Content-Type, Authorization, X-Requested-With
Access-Control-Allow-Methods:GET, PUT, POST, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Max-Age:1000
Cache-Control:no-cache
Connection:close
Content-Length:5
Content-Type:text/html; charset=UTF-8
Date:Mon, 24 Feb 2014 11:57:54 GMT
Server:Apache/2.2.3 (CentOS)
X-Powered-By:PHP/5.1.6
了Serverside腳本:
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Cache-Control: no-cache');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
if (!empty($_FILES)) {
與Firefox的響應與該OPTIONS
方法的要求是空的,並且沒有以下POST
請求。
下面是Firefox的標題:
我想不通爲什麼它不與Firefox和IE8工作。
感謝您的幫助。
我剛剛嘗試過使用Flash運行時:它與Chrome和IE 10一起使用,但與Firefox和IE8不兼容。奇怪的是,該alert(info['runtime']);
不再出現,但沒有JavaScript錯誤控制檯...
我不確定,但我認爲某些瀏覽器可能會向服務器發送預檢檢查請求以檢查標題(我對於確切的細節有點模糊),可能會在此處提供一些信息:http:// stackoverflow.com/questions/13614802/jquery-ajax-request-called-twice-and-the-first-request-does-not-send-token-in-he –