Why do plugins that cause ‘unexpected output’ create AJAX problems?

The HTTP Protocol that runs both page loading, file downloading and AJAX functionality requires a headers (multiple lines) consisting of Name: Value and TWO ENTERS (separator) and then the actual data.

Headers are very important as they control what the browser should do with the received data and state what the data is. AJAX needs to send headers that specify the Content-Type of delivered data, even Content-Length. It can be TEXT/HTML/XML/JSON/JS… etc. Regardless, bad headers can lead to bad data decoding. Browsers should not guess what you’re sending. They should know exactly based on what the headers advertise.

That’s why AJAX requires sending some special headers. But headers can not be sent after output, of any kind. So, using the header() function in PHP generates errors if you do it after data was sent. It’s PHP’s attempt to tell you that special instructions you need to send to the browser before actual data are not delivered. This causes a chain reaction as the error output will break the XML/JSON consistency of delivered data. This, in turn, breaks the AJAX functionality as expected XML/JSON can not be decoded (it contains illegal structures generated by the errors generated by the premature output).

This is why it’s very important that plugins don’t start/end with spaces. If you have output problems, use the headers_sent() function and it will allow you to discover where output has started and fix the offending code.

I’m pretty sure I made no sense, but Web Developers should understand the underlying protocols that power the web. Not low-levels like TCP-IP/ICMP… but higher-level like HTTPWe work at this level!

Regards.