0

I'm attempting to use jQuery to load the contents of a plaintext (.bib) file. Despite my best attempts, the file seems to be interpreted as XML, leading to a parsing error.

Code:

$(document).ready(function(){
    $.ajax({
        url: "publications.bib",
        contentType: "text/plain",
        dataType: "text",
        type: "GET",
        success: function(response) {
            console.log(response);
        }
    }, 'text');
});

As you can see, I'm desperately trying to make it interpret it as text and not XML. How do I fix this?

The full error is:

XML Parsing Error: not well-formed
Location: http://localhost/publications.bib
Line Number 1, Column 1:
Oxonon
  • 281
  • 2
  • 8
  • 1
    What is the `Content-type:` header that the server is sending? – Barmar Feb 24 '23 at 16:06
  • Apologies, how would I check this? I added success: function(response, status, xhr) { console.log(response); console.log(xhr.getResponseHeader("content-type")); }, which returned null – Oxonon Feb 24 '23 at 16:11
  • Use the Network tab in Developer Tools to see the response headers. – Barmar Feb 24 '23 at 16:12
  • Content-Type text/plain, but Type xml (from network tab) – Oxonon Feb 24 '23 at 16:14
  • 1
    Note that the `contentType:` option to `$.ajax()` makes no difference in a `GET` request, since it doesn't send any contents. – Barmar Feb 24 '23 at 16:16
  • 1
    You're sure you saw `text/plain` in the **Response** headers? – Barmar Feb 24 '23 at 16:20
  • Does this answer your question? [Why does jQuery insist my plain text is not "well-formed"?](https://stackoverflow.com/questions/1828228/why-does-jquery-insist-my-plain-text-is-not-well-formed) – BDL Mar 02 '23 at 09:23

1 Answers1

1

I have created a minimal test case based on your code. Apparently it's the file extension which makes jquery think it's an XML.

Replaced .bib with a .txt and it works as you want.

$.ajax( {
    url: "pub.txt",
    contentType: "text/plain",
    dataType: "text",
    type: "GET",
    success: function ( response ){
        console.log( response );
    }
}, 'text' );
RudyMartin
  • 68
  • 8