无论如何。我不得不更深入地挖掘 JSJaC 库并对代码进行一些注入。但首先我做了一些解决方法。基本上我在响应中添加了以下标题
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type, *
通常这允许使用本机 xhr 进行跨域请求。然而,事实证明它只适用于现代浏览器。例如,它在 IE8 中不起作用,并且任何版本的 Opera 都简单地拒绝了此标头。
然后我使用了基于闪存的解决方案。我使用了flXHR,并像这样修改了jsjac.uncompressed.js。
XmlHttp.create = function () {
// try {
// if (window.XMLHttpRequest) {
// var req = new XMLHttpRequest();
//
// // some versions of Moz do not support the readyState property
// // and the onreadystate event so we patch it!
// if (req.readyState == null) {
// req.readyState = 1;
// req.addEventListener("load", function () {
// req.readyState = 4;
// if (typeof req.onreadystatechange == "function")
// req.onreadystatechange();
// }, false);
// }
//
// return req;
// }
// if (window.ActiveXObject) {
// return new ActiveXObject(XmlHttp.getPrefix() + ".XmlHttp");
// }
// }
// catch (ex) {}
// // fell through
// throw new Error("Your browser does not support XmlHttp objects");
var AsyncClient = new flensed.flXHR({
"autoUpdatePlayer": true,
"instanceId": "myproxy" + _xhrpf.toString(),
// This is important because the library uses the response xml of the object to manipulate the data
"xmlResponseText": true,
"onreadystatechange": function () { }
});
// counter for giving a unique id for the flash xhr object.
_xhrpf++;
return AsyncClient;
};
var _xhrpf = 1;
然后我只是在目标域的根目录中添加了一个crossdomain.xml。现在,如果浏览器有 flash 插件,它可以完美运行。
另外,如果没有flash插件,我想做一些检测机制,只做一个原生的xhr,希望浏览器支持跨域请求的headers。