【发布时间】:2012-04-02 13:00:34
【问题描述】:
这是一个与this one有关的问题。
在 UPDATE II 中,我根据 Jamie 的反馈添加了一个脚本。
更新 - tl;dr:
我创建了一个带有临时密钥的小提琴,以便你们更容易看到问题:http://jsfiddle.net/S6wEN/。
由于这个问题太长,这是一个摘要。
- 我尝试使用 imgur API 通过跨域 XHR 更新图像。
- 为了在实现中抽象出细节,我使用了 Jquery Form Plugin(显然,它包含在 fiddle 中)。
- 在 Chrome、Firefox 等中运行良好,但在 IE9 中无法运行。
- 预期结果是更新图像并检索图像类型。
您仍然可以在下面找到详细信息。
谢谢
我有这个 HTML:
<body>
<form id="uploadForm" action="http://api.imgur.com/2/upload.xml" method="POST" enctype="multipart/form-data">
<input type="hidden" name="key" value="MYKEY">
File: <input type="file" name="image">
Return Type: <select id="uploadResponseType" name="mimetype">
<option value="xml">xml</option>
</select>
<input type="submit" value="Submit 1" name="uploadSubmitter1">
</form>
<div id="uploadOutput"></div>
</body>
所以基本上,我有一个表单可以通过跨域 XHR 将图像上传到 imgur。为了管理讨厌的细节,我使用Jquery Form Plugin,效果很好。但是,当我尝试向 imgur 发送图像并接收 xml 响应时,它在 IE9 中无法按预期工作(我尚未在 IE8 中进行测试,但我不希望有好消息)。它在 Chrome 和 Firefox 中运行良好。这是javascript部分:
(function() {
$('#uploadForm').ajaxForm({
beforeSubmit: function(a,f,o) {
o.dataType = $('#uploadResponseType')[0].value;
$('#uploadOutput').html('Submitting...');
},
complete: function(data) {
var xmlDoc = $.parseXML( data.responseText ),
$xml = $( xmlDoc );
$('#uploadOutput').html($xml.find('type'));
}
});
})();
在 IE9 中我收到以下错误:
SCRIPT5022: Invalid XML: null
jquery.min.js, line 2 character 10890
XML5619: Incorrect document syntax.
, line 1 character 1
我还使用了 Jquery Form Plugin 页面中给出的示例,该示例仅使用 Javascript,但没有帮助。显然,引用 Jquery 的第一个错误消失了,但我无法获得预期的结果(在这种情况下,id="uploadOutput" 的 div 中的 image/jpeg )。
当我在 IE9 中查看控制台时,我明白了:
URL Method Result Type Received Taken Initiator Wait Start Request Response Cache read Gap
http://api.imgur.com/2/upload.xml POST 200 application/xml 1.07 KB 7.89 s click 2808 93 5351 0 0 0
作为身体反应:
<?xml version="1.0" encoding="utf-8"?>
<upload><image><name/><title/><caption/><hash>xMCdD</hash>
<deletehash>Nb7Pvf3zPNohmkQ</deletehash><datetime>2012-03-17 01:15:22</datetime>
<type>image/jpeg</type><animated>false</animated><width>1024</width
<height>768</height><size>208053</size><views>0</views><bandwidth>0</bandwidth></image
<links><original>http://i.imgur.com/xMCdD.jpg</original
<imgur_page>http://imgur.com/xMCdD</imgur_page>
<delete_page>http://imgur.com/delete/Nb7Pvf3zPNohmkQ</delete_page>
<small_square>http://i.imgur.com/xMCdDs.jpg</small_square>
<large_thumbnail>http://i.imgur.com/xMCdDl.jpg</large_thumbnail></links></upload>
这一切都很好,但由于某种原因,我无法将这些信息处理到 HTML 页面中。我验证了 XML,以确保这不是问题。当然是有效的。
那么,IE9 有什么问题?
更新:
另一种在 Chrome 和 Firefox 中有效但在 IE9 中无效的获取 XML 的方法:
(function() {
$('#uploadForm').ajaxForm({
dataType: "xml",
beforeSubmit: function(a,f,o) {
o.dataType = $('#uploadResponseType')[0].value;
$('#uploadOutput').html('Submitting...');
},
success: function(data) {
var $xml = $( data ),
element = $($xml).find('type').text();
alert(element);
}
});
})();
更新 2:
<!DOCTYPE html>
<html>
<body>
<form id="uploadForm" action="http://api.imgur.com/2/upload.xml" method="POST" enctype="multipart/form-data">
<input type="hidden" name="key" value="00ced2f13cf6435ae8faec5d498cbbfe">
File: <input type="file" name="image">
Return Type: <select id="uploadResponseType" name="mimetype">
<option value="xml">xml</option>
</select>
<input type="submit" value="Submit 1" name="uploadSubmitter1">
</form>
<div id="uploadOutput"></div>
</body>
</html>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script>
(function() {
var options = {
// target: '#output1', // target element(s) to be updated with server response
//beforeSubmit: showRequest, // pre-submit callback
success: afterSuccess, // post-submit callback
complete: afterCompletion,
// other available options:
//url: url // override for form's 'action' attribute
type: 'POST', // 'get' or 'post', override for form's 'method' attribute
dataType: 'xml' // 'xml', 'script', or 'json' (expected server response type)
//clearForm: true // clear all form fields after successful submit
//resetForm: true // reset the form after successful submit
// $.ajax options can be used here too, for example:
//timeout: 3000
};
function process_xml(xml) {
var type = $(xml).find('type').text() ;
return type;
// Find other elements and add them to your document
}
function afterSuccess(responseText, statusText, xhr, $form) {
// for normal html responses, the first argument to the success callback
// is the XMLHttpRequest object's responseText property
// if the ajaxForm method was passed an Options Object with the dataType
// property set to 'xml' then the first argument to the success callback
// is the XMLHttpRequest object's responseXML property
// if the ajaxForm method was passed an Options Object with the dataType
// property set to 'json' then the first argument to the success callback
// is the json data object returned by the server
var $xml = process_xml(responseText);
console.log('success: ' + $xml);
}
function afterCompletion(xhr,status){
if(status == 'parsererror'){
xmlDoc = null;
// Create the XML document from the responseText string
if(window.DOMParser) {
parser = new DOMParser();
xml = parser.parseFromString(xhr.responseText,"text/xml");
} else {
// Internet Explorer
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = "false";
xml.loadXML(xhr.responseText);
}
}
console.log('complete: ' + process_xml(xhr.responseText));
}
$('#uploadForm').ajaxForm(options);
})();
</script>
提前致谢。
【问题讨论】:
-
您是否使用 Fiddler2 运行 IE 来检查结果。 IE 在内容编码标头等方面可能非常严格......我怀疑虽然文档在文档中标记为 UTF-8,但服务器可能没有指定字符集。其他浏览器通常会默认使用 UTF-8。
-
我这样做了,结果也是一样。我没有在这里发布它,因为它需要一个密钥才能工作,所以在这种情况下它没有实际用途。不过,我会稍后再试。
-
我刚刚添加了一个带有临时密钥的小提琴。
-
顺便说一句,我不确定这是否与编码有关,因为此处给出的示例 (malsup.com/jquery/form/#file-upload) 可以正常工作。
-
您发布的 XML 无效:第 5 行,“”没有结束“>”。或者这只是一个发布错字?
标签: javascript jquery xml ajax internet-explorer-9