django - Latin1/UTF-8 Encoding Problems in AngularJS -
i have python 2.7 django + angularjs app. there's input field feeds data model , data sent server using angular's $http. when input field contains character "é", django doesn't it. when use "★é" django has no problem it. seems me star character being outside latin1 charset forces encoding utf-8, while when non-latin character "é", angular sends data latin1, confuses python code.
the error message django is: unicodedecodeerror: 'utf8' codec can't decode byte 0xe9 in position 0: invalid continuation byte
telling simplejson.loads() function on server read data using iso-8859-1 (latin1) encoding worked fine when input string contained é in , no star, proves data coming browser latin1 unless forced utf-8 non-latin1 characters, star.
is there way tell angular send data using utf-8?
the angular code sends data server:
$http({ url: $scope.dataurl, method: 'post', data: json.stringify({recipe: recipe}), headers: {'content-type': 'application/json'} }).success(...).error(...);
the django code reads data:
recipe = simplejson.loads(request.raw_post_data)['recipe']
i found 1 way works, using transformrequest config parameter.
transformrequest: function (data, headersgetter) { return encode_utf8(json.stringify(data)); } function encode_utf8(s) { return unescape(encodeuricomponent(s)); }
i'm using encode function found , explained @ http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html , json library found @ http://www.json.org/json2.js.
Comments
Post a Comment