我正在尝试使用goog.net.IFrameIO上传文件,并在处理完上传文件后从服务器获取JSON响应。它在Chrome和Safari上工作正常,但不能与IE9一起使用,其中“Acecss拒绝内容文档”错误,然后提示保存/打开json响应。这是我的html代码和js代码 -
HTML代码:
<form action="/" enctype="multipart/form-data" method="POST" id="upload-form">
<input type="file" name="selected-file" id="selected-file">
<div role="button" class="test-button>Upload</div>
</form>
JS代码:
test.prototype.uploadFile = function() {
// Send the form submission to the server using IframeIo to make it feel like
// AJAX.
var form = /** @type {HTMLFormElement} */ (goog.dom.getRequiredElement(
'upload-form'));
var io = new goog.net.IframeIo();
goog.events.listen(io, goog.net.EventType.COMPLETE, goog.bind(this.processResponse_, this, io));
io.sendFromForm(form, '/uploadfile');
}
test.prototype.processResponse = function(io) {
if (!io.isSuccess()) {
alert('iframeio error encountered:' + io.getLastError());
return;
}
var response = null;
try {
response = io.getResponseJson();
..
} catch (error) {
....
}
我只是尝试使用闭包解决方案,而不是其他库。我也在Fallback AJAX file upload for Internet Explorer处理了这个问题,但无法就其接受的答案发表评论。
[故障排除]:我看到goog.net.IframeIo.prototype.onIeReadyStateChange_()的方法被调用,具有就绪状态,如加载,交互,然后完成,但无法从为IE创建的iframe获取内容文档
HTTP请求标头: 接受:text / html,application / xhtml + xml, / 内容类型:multipart / form-data Accept-Encoding:gzip,deflate
HTTP响应标头: X-Frame-Options:SAMEORIGIN Content-Type:application / json;字符集= UTF-8
答案 0 :(得分:0)
IE的IframeIO库(版本低于11)创建一个iframe,在文件上传后将HTTP响应发布到该iframe。
但IE9 / 10中的iframe(不确定8或11)不接受内容类型为JSON的HTTP响应。对于IE9 / 10,响应内容类型需要更改为“text / plain”才能使其正常工作。