下面是一个XDomainRequest,它是从javascript到另一个子域上的Nginx上的PHP后端。返回结果总是执行错误函数,XDomainRequest
不提供调试详细信息。代码有问题吗?
的Javascript
var xdr = new XDomainRequest();
xdr.open(method.toLowerCase(), url);
timeout = 10000;
// Required to XDomainRequest works
xdr.timeout = timeout;
xdr.onprogress = function() {};
xdr.ontimeout = function() {
completeRequest(callback, 408, 'Timeout', 'Content-Type: text/plain');
xdr.abort();
};
xdr.onload = function() {
completeRequest(callback, 200, xdr.responseText, 'Content-Type: ' + xdr.contentType);
};
xdr.onerror = function() {
completeRequest(callback, 500, 'Error', 'Content-Type: text/plain');
xdr.abort();
};
$browserDefer(function () {
xdr.send();
}, 0); //fix IE bug that raises '$apply already in progress' on cached requests
if (timeout > 0) {
$browserDefer(function() {
status = -1;
xdr.abort();
}, timeout);
}
使用PreFlight选项的PHP检查
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && ($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'POST' || $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'DELETE' || $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'PUT' )) {
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
header("Access-Control-Allow-Credentials: true");
//header('Access-Control-Allow-Headers: *,X-Requested-With,Content-Type');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT'); // http://stackoverflow.com/a/7605119/578667
header('Access-Control-Max-Age: 86400');
echo PVResponse::createResponse(200, 'Successful Connection');
}
exit();
}
header('Access-Control-Allow-Origin: '. $_SERVER['HTTP_ORIGIN'] );
header('Access-Control-Allow-Credentials: true' );
header('Access-Control-Request-Method: *');
header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: *,x-requested-with,Content-Type');
header('X-Frame-Options: DENY');
//Execute rest of PHP code after headers have been set
我已经跨子域调用在其他所有浏览器中工作。 IE8和IE9有什么特别的东西,比如特殊的Allow-Headers
或者其他我不想要的东西吗?