我在文档元素"之后得到"垃圾尝试通过jQuery ajax
调用加载文件时。 2010年Xml Calling with jQuery, (invalid XML)的答案似乎表明dataType: "text"
代替dataType: "xml",
应该修复它,但这对我没有用。
以下是我在Firefox上本地运行的测试(" I think Firefox permits access to local files via XHR"," Firefox can disabled that within it's about:config
"):
test.txt
<option value='1'>Hello</option>
<option value='2'>World</option>
test.htm
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script type="text/javascript">
$.ajax ({
url: 'test.txt',
data: {},
type: 'get',
datatype: 'text', // 'html', // "junk after document element" seems to be caused by both text and html?
cache: 'false',
success: function(result) {
if (jQuery.type(result) == "object") {
console.log("[none?!]");
} else {
console.log(result);
}
},
error: function(xhr, ajaxOptions, thrownError){
console.log("ERROR: " + xhr.status + " / " + thrownError + " / " + xhr.responseText);
}
}); // end ajax
</script>
</head>
<body>
</body>
</html>
Firefox 39浏览器控制台中的响应是:
"ERROR: 200 / Error: Invalid XML: <option value='1'>Hello</option>
<option value='2'>World</option>
/ <option value='1'>Hello</option>
<option value='2'>World</option>
" test.htm:20:7
junk after document element test.txt:2:3
junk after document element test.htm:2:3
奇怪的是,我显式地状态datatype: 'text'
只是为了让JavaScript避免将这个字符串解析为XML - 然而它仍然试图这样做,并且失败了?为什么会这样,如何让AJAX调用 而不是 来将响应解析为HTML / XML?
dataType: 'text',
...现在至少读取的内容并没有注册为错误 - 控制台日志是:
" <option value='1'>Hello</option>
<option value='2'>World</option>
" test.htm:16:9
junk after document element test.txt:2:3
...但我仍然在文件元素&#34;之后得到了#;;这可以以某种方式避免吗?