Hi all, hoping that someone can shed some light on this.
I am creating an HTTPClient call to load a JSON string. I set the content type and charset as follows:
theClient = Ti.Network.createHTTPClient();
theClient.open('GET', url);
theClient.setRequestHeader('Content-Type', 'application/json; charset=utf-8;');
theClient.send();
theClient.onload = function()
{
var theText = this.responseText;
var theData = this.responseData;
}
This works fine for most calls but if the JSON contains any unusual characters, mainly the £ sign, the responseText just returns 'null'.
I can only assume that this is because it doesn't understand the character, maybe i'm not setting the charset correctly?
Any thoughts or longshots are welcome as i've tried everything i can think of :-)
As an aside...strangely the responseData returns an array of undefined variables corresponding to the number of characters in the expected response. For example if the response was simply 'hello£' the responseText would be null and the responseData would be an array of 6 variables all of which are undefined.
Thanks for any thoughts
Martyn
1 Answer
Accepted Answer
Hi Martyn
You will need to encode any characters that fall outside the common ASCII table into Unicode equivalents. Consider pretty much anything in the top half of the ASCII table 128-255 as something you should encode.
Examples;
POUND = £ becomes \u00a3 HALF = ½ becomes \u00bd QUARTER = ¼ becomes \u00bcUsed in your JSON or strings inside your code;
"\u00a310.50" = "£10.50"Somewhat of a pain but once you build a filter the problem goes away. Just remember to use the Unicode hex values and not ASCII versions.
Your Answer
Think you can help? Login to answer this question!