开发者

Strange characters at beginning of XML AJAX response?

开发者 https://www.devze.com 2023-02-07 06:28 出处:网络
I\'m making multiple AJAX calls that returns XML data.When I get the data back, my success function (in JQuery) tries to turn the XML to JSON (using a plugin).I was quickly reminded why I can\'t assum

I'm making multiple AJAX calls that returns XML data. When I get the data back, my success function (in JQuery) tries to turn the XML to JSON (using a plugin). I was quickly reminded why I can't assume I would be getting VALID XML back from my AJAX request -- because it turns out a few of the XML responses were invalid -- causing the JSON conversion to fail, script to fail, etc...

My questions are:

  1. What is the best way to check for valid XML on an AJAX response? Or, should I just attempt the JSON conversion, then do a quick check if the JSON object is valid?
  2. In troubleshooting the XML, I found that there are a few strange characters at the VERY beginning of the XML response. Here's an image from my Firebug:

Strange characters at beginning of XML AJAX response?

Should I try to detect and strip the response of those chars or could there possibly be something w开发者_如何学JAVArong with my encoding?

Any help is appreciated! Let me know if more info is needed!


It's the UTF-8 byte-order mark when incorrectly interpreted as ISO-8859-1.

You can't safely strip this because it's just a symptom of a larger problem. Your content is encoded as UTF-8. Somewhere along the way you are decoding it as ISO-8859-1 instead. If you try to hide the problem by stripping the BOM, you're only setting yourself up for more problems down the line as soon as you start using non-ASCII characters. The only reason things are even looking sort-of right is because ASCII is a common subset of both UTF-8 and ISO-8859-1.


The strange characters are the Byte Order Mark and are actually valid XML, you can most likely just strip them without risk in most circumstances.

0

精彩评论

暂无评论...
验证码 换一张
取 消