最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery ajax() function is ignoring dataType parameter in Firefox - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Apr 24, 2018 at 8:52 Ignacio Ara 2,5802 gold badges27 silver badges37 bronze badges asked Jul 2, 2013 at 16:38 ccleveccleve 15.8k29 gold badges101 silver badges172 bronze badges 9
  • 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 Commented Jul 2, 2013 at 16:46
  • 1 Thanks, but it's not XML. It's HTML. – ccleve Commented Jul 2, 2013 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 remend you console the result as console.log($('<div />').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 Commented Jul 2, 2013 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 Commented Jul 2, 2013 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 Commented Jul 2, 2013 at 17:15
 |  Show 4 more ments

3 Answers 3

Reset to default 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./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.

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.

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

发布评论

评论列表(0)

  1. 暂无评论