Javascript Chrome扩展将不可读的数据发送到python脚本

Javascript Chrome扩展将不可读的数据发送到python脚本,javascript,google-app-engine,encoding,utf-8,google-chrome-extension,Javascript,Google App Engine,Encoding,Utf 8,Google Chrome Extension,我是Chrome扩展的新手,刚创建了一个弹出窗口,当通过Javascript提交时,它会将信息发送到GAE上的Python脚本,该脚本可以处理数据。现在,只要我不使用像Ä、Ö、Ü这样的特殊字符,一切都会很好地工作。当我使用这些字母时,我会得到错误: Traceback (most recent call last): File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", lin

我是Chrome扩展的新手,刚创建了一个弹出窗口,当通过Javascript提交时,它会将信息发送到GAE上的Python脚本,该脚本可以处理数据。现在,只要我不使用像Ä、Ö、Ü这样的特殊字符,一切都会很好地工作。当我使用这些字母时,我会得到错误:

Traceback (most recent call last):
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in     default_dispatcher
    return route.handler_adapter(request, response)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
  File "/base/data/home/apps/s~google.com:finaggintel/1.368063289009985228/main.py", line 115, in post
t.title = self.request.get('title').encode('utf-8')
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 175, in get
param_value = self.get_all(argument_name)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 212, in get_all
param_value = self.params.getall(argument_name)
  File "/python27_runtime/python27_lib/versions/third_party/webob-1.1.1/webob/multidict.py", line 327, in getall
return map(self._decode_value, self.multi.getall(self._encode_key(key)))
  File "/python27_runtime/python27_lib/versions/third_party/webob-1.1.1/webob/multidict.py", line 301, in _decode_value
value = value.decode(self.encoding, self.errors)
  File "/python27_runtime/python27_dist/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xdc in position 0: unexpected end of data    
坦率地说,我不知道在哪里调试这个问题。我尝试了用Python进行utf-8的de和编码(但是,这对我来说还是个新鲜事):

我做错什么了吗?我甚至接近这个问题了吗?谢谢你的帮助

编辑:包括回溯

确定

如果将其从后到前,则应将内嵌数据解码为unicode表示形式

e、 g

那你的台词呢

t.title = self.request.get('title').encode('utf-8')
试一试

然而,这假设数据需要从utf-8流解码

您应该在表单中(或在发布时在客户端上)指定
accept charset=“utf-8”
,以便定义正确的编码,而不是猜测和尝试解码


例如,在windows上,默认编码不是utf-8,而是latin_1,尝试从latin_1解码utf-8是行不通的。如果使用decode('latin_1')

可以解码解码失败的字符('utf-8')(0xdc),包括堆栈跟踪,并让我们知道代码中的错误位置数据来自何处,是真正的utf-8还是Unicode。您是否在表单中设置了accept charset=“utf-8”?0xdc似乎是它的拉丁字母1。“接受字符集”部分对我来说是新的,但它只处理了一个新错误:“UnicodeEncodeError:'ascii'编解码器无法对位置0处的字符u'\xdc'进行编码:序号不在范围(128)”内。也许这有帮助?顺便说一句:我在GAE上使用相同的处理和完全相同的形式(不是Chrome扩展),它工作得非常好。不管怎样-它与accept字符集一起工作,只是忘记了取出解码!谢谢,你能编辑你的答案吗?这样我就可以接受了?
>>> x = "Ä"
>>> x.decode('utf-8')
u'\xc4'
>>> 
>>> y=x.decode('utf-8')
>>> print y
Ä
>>> 
t.title = self.request.get('title').encode('utf-8')
t.title = self.request.get('title').decode('utf-8')