I am getting "junk after document element" when trying to load a file via jQuery ajax
call. This answer from 2010 Xml Calling with jQuery, (invalid XML) seems to suggest that dataType: "text"
instead of dataType: "xml",
should fix it, but that doesn't work for me.
Here is the test I run locally on 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>
The response in Firefox 39 Browser Console is:
"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
The weird thing is, I explicitly state datatype: 'text'
just so that JavaScript avoids parsing this string as XML - and yet it still attempts to do so, and fails? Why is this, and how can I get the AJAX call not to parse the response as HTML/XML?
EDIT: Thanks to @JAAulde's comment, now I know I've made a spelling mistake; so when I try with the correct case:
dataType: 'text',
... now at least the read doesn't register as an error - the console log is:
" <option value='1'>Hello</option>
<option value='2'>World</option>
" test.htm:16:9
junk after document element test.txt:2:3
... but I'm still getting "junk after document element"; can this somehow be avoided?