21

I'm trying to use jQuery.ajax() to fetch some html, but Firefox is giving me a "junk after document element" error message. As explained here and here the problem seems to be that Firefox is expecting XML from the server, and when it doesn't parse correctly it throws the error. Here's my ajax code:

jQuery.ajax({
    url: name,
    dataType: "html",
    success: function(result) {
        console.log(result);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.log(errorThrown);
    }
});

The server returns the html with these response headers:

Accept-Ranges   bytes
Content-Length  2957
Last-Modified   Tue, 02 Jul 2013 16:16:59 GMT

Note that there's no content-type header. I'm sure that adding one would solve the problem, but that's not an option.

The real problem is that Firefox appears to be ignoring the dataType: parameter in the ajax call. I've also tried adding contentType: and accepts: parameters, but it doesn't help.

What am I missing here? How do I force Firefox to process the response as plain text?

Ignacio Ara
  • 2,476
  • 2
  • 26
  • 37
ccleve
  • 15,239
  • 27
  • 91
  • 157
  • 1
    RoR ... FireFox ... bleh, sounds like you're fighting the new IE in browsers and the ... well, never anything nice to say about RoR. I still don't see a use in it. Much as I hate to say it, this is either an issue with `Content-type` header or you need to double check, maybe var_dump in another browser, or write it to file, but double check you XML is in it's proper schema – SpYk3HH Jul 02 '13 at 16:46
  • 1
    Thanks, but it's not XML. It's HTML. – ccleve Jul 02 '13 at 16:47
  • 1
    Ah, I misread. hmm .... have you tried a plain and simple `echo` of the HTML as string? Of course, I'd recommend you console the result as `console.log($('
    ').html(result))`. That way you see it as a jQuery object instead of a giant string in your console. That also gives you the ability to parse through the HTML using `.find`
    – SpYk3HH Jul 02 '13 at 16:52
  • 1
    @ccleve try setting `dataType : 'text'` for plaintext. Failing that, try `dataType : 'xml text'`, which tells jQuery to convert all XML to plaintext (worth a shot). What version of jQuery are you using? – MrCode Jul 02 '13 at 16:55
  • I just tried it. 'text' gives the same error. 'xml text' gives the same error twice, which is odd. I was using jQuery 1.9.1, but I just upgraded to 2.0.2 and it yields the same error. – ccleve Jul 02 '13 at 17:15
  • One other thought, I know you're in RoR, but you still have `json_encode`? Try `echo(json_encode($html));` and set your `dataType` to either `text` or `json`. Just try it. Been a long while since i've RoRd, so I'm grasping at old skool PhP straws here, but something to try. See the result. – SpYk3HH Jul 02 '13 at 17:20
  • I'm not in RoR. As I mention in the post, setting the content-type on the server side solves the problem, but it's not an option here. – ccleve Jul 02 '13 at 17:34
  • @ccleve I would try playing around with the `converters` option. Try `converters : {"text xml" : true}`, failing that try: `converters : {"text xml" : function(value){ return value; }}`. The converters lets you override jQuery's default converter, so maybe you can override the XML converter to return the plain text instead of the default of using `$.parseXML()`. – MrCode Jul 03 '13 at 09:13
  • I'm experiencing the same issue 6 year later... – Peter May 23 '19 at 21:21

3 Answers3

1

The above code is working fine.

you can use the below code. As I noticed your file should be saved in .txt format.

 jQuery.ajax({
    url: "https://www.w3schools.com/jquery/demo_test.txt",
    dataType: "html",
    success: function(result) {
        console.log(result);
        const parser = new DOMParser();
        const res = parser.parseFromString(result, 'text/html');
        console.log(res);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.log(errorThrown);
    }
});

I have tested this code in Firefox and it's working fine.

enter image description here

Negi Rox
  • 3,828
  • 1
  • 11
  • 18
0

How does the HTML response look? If it doesn't already, I would try and make sure that the response starts with a doctype declaration on the first line, as in <!doctype html>.

With a bit of luck, this could bring Firefox' content type detection onto the right track.

janfoeh
  • 10,243
  • 2
  • 31
  • 56
-3

Alright, so you can try "HTML" instead of "html".

Ashish Kumar
  • 2,991
  • 3
  • 18
  • 27