Michał Ziobro
Michał Ziobro

Reputation: 11822

HTTP Content Type in Header

I have old web app that generates XML files in php. This XMLs are requested by XMLHttpRequest object (AJAX). Everything works correctly. But today there has been some server upgrade and web app breaks down a little.

The problem is that in code there are checks related to XMLHttpRequests. 1) if I have a response than I parse it properly based on it content type.

var contentType = xhr.getResponseHeader("Content-Type"); 
        //build the json object if the response has one
        if(contentType == "application/json") { 
            response = JSON.parse(xhr.responseText); 
        }
        //get the dom element if the response is XML
        else if(contentType == "text/xml") { 
            response = xhr.responseXML; 
        } else { //by default get the response as text
            response = xhr.responseText; 
        }

And here is the problem cause server now returns:

text/xml;charset=UTF-8

instead of

text/xml

Ok I can just change this line and the error disappear. But I would like to know why server upgrade (bluehost) can have influance on this.

This is PHP/MySQL environment.

Upvotes: 2

Views: 1032

Answers (2)

VKK
VKK

Reputation: 912

Just to add to Steve E's answer, the "charset=UTF-8" portion is specifying a character set.

There is no better explanation of unicode (UTF-8 is an implementation of unicode) and character sets then the one on Joel on Software, here (incidentally Joel also created Stack Overflow). In short, character sets define the set of characters than can be used in text. Unicode, a character set, supports nearly all international languages. UTF-8 specifies how the Unicode character set is implemented in bytes (so with UTF-8, Unicode characters take anywhere from 1 - 4 bytes). When you see garbled text (for example ?s instead of characters) that is often because the document is not being interpreted in the correct character encoding.

It's actually best practice to include the encoding in the content-type header, so I would keep it as "text/xml;charset=UTF-8". Bluehost was likely updating their default settings (ie/ the default content-type they display for xml documents) which caused the change. Just as an aside, the terms character set and encoding are sometimes used interchangeably, but when you specify "charset=UTF-8" you are more correctly specifying the encoding (UTF-8 is the encoding, Unicode is the character set).

Upvotes: 1

Steve E.
Steve E.

Reputation: 9353

Both are valid content types. The content type can be set by the web server software (e.g. Apache) or the script (PHP). I'm assuming it's PHP because of the tag on your question.

If you control the script on the server and want to specify the content type, it's easy to do within PHP by adding the line:

header('Content-Type: text/xml');

This must occur before any other output is sent from the script because headers appear before content in http responses. If the header is not set within the PHP script, then the web server will choose one instead.

If you don't control the script that produces the XML or the server then you just need to accept that it is common for systems to be upgraded and this may impact on your own application.

Upvotes: 1

Related Questions