简体   繁体   English

Appengine ProtoRPC无法解码我的JSON

[英]Appengine ProtoRPC can't decode my JSON

Can't seem to get protoRPC API on app-engine to work for me. 应用引擎上的protoRPC API似乎无法为我工作。

This is my request: 这是我的要求:

$.ajax({
    url: '/guestRPC.get_tags',
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json',
    data: {
            prefix: JSON.stringify(request),
            locale: JSON.stringify('{{locale}}')
    },
    success: somefunction
});

This is what I'm sending according to the browser debugger: 这是我根据浏览器调试器发送的内容:

Request Method:POST
Status Code:500 Internal Server Error
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,he;q=0.6
Connection:keep-alive
Content-Length:51
Content-Type:application/json
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.137 Safari/535.19
X-Requested-With:XMLHttpRequest
Request Payload
prefix=%7B%22term%22%3A%22%22%7D&locale=%22en_US%22
Response Headersview source
Cache-Control:no-cache
Content-Encoding:gzip
Content-Length:87
Date:Mon, 26 Mar 2012 18:58:24 GMT
Expires:Fri, 01 Jan 1990 00:00:00 GMT
Server:Google Frontend
Vary:Accept-Encoding
content-type:application/json
x-content-type-options:nosniff

And this is the error on server: 这是服务器上的错误:

2012-03-26 21:56:02.161 /guestRPC.get_tags 500 152ms 0kb Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.137 Safari/535.19
- - - [26/Mar/2012:11:56:02 -0700] "POST /guestRPC.get_tags HTTP/1.1" 500 238 "mysite" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.137 Safari/535.19" "mysite" ms=152 cpu_ms=0 api_cpu_ms=0 cpm_usd=0.000138 pending_ms=86 instance=...
D2012-03-26 21:56:02.155
Entered guestRPC handler.
E2012-03-26 21:56:02.156
An unexpected error occured when handling RPC: No JSON object could be decoded: line 1 column 0 (char 0)
Traceback (most recent call last):
  File "/base/python_runtime/python_lib/versions/1/protorpc/webapp/service_handlers.py", line 601, in handle
    request = mapper.build_request(self, method_info.request_type)
  File "/base/python_runtime/python_lib/versions/1/protorpc/webapp/service_handlers.py", line 235, in build_request
    return self.__protocol.decode_message(request_type, handler.request.body)
  File "/base/python_runtime/python_lib/versions/1/protorpc/protojson.py", line 156, in decode_message
    dictionary = json.loads(encoded_message)
  File "/base/python_runtime/python_lib/versions/1/simplejson/__init__.py", line 388, in loads
    return _default_decoder.decode(s)
  File "/base/python_runtime/python_lib/versions/1/simplejson/decoder.py", line 402, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/base/python_runtime/python_lib/versions/1/simplejson/decoder.py", line 420, in raw_decode
    raise JSONDecodeError("No JSON object could be decoded", s, idx)
JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)
E2012-03-26 21:56:02.159
Internal Server Error

The problem that is happening here can be seen if you look at the "Request Payload" section. 如果您查看“请求有效负载”部分,则可以看到此处发生的问题。 It turns out that when you use .ajax to send a request it will not put outer '{' and '}' in your request. 事实证明,当您使用.ajax发送请求时,它不会在请求中添加外部“ {”和“}”。 Also, notice that the values are separated by an &. 另外,请注意,值之间用&分隔。 This means that .ajax turned your nice object in to a urlencoded request, which is not what you wanted. 这意味着.ajax将您的好对象转换为经过urlencoded的请求,这不是您想要的。

The reason is that .ajax the "dataType" parameter only refers to how the .ajax function will handle the request content, not what it will do to the content when sending. 原因是.ajax的“ dataType”参数仅表示.ajax函数如何处理请求内容,而不是发送时对内容的处理方式。 .ajax always sends a query string. .ajax始终发送查询字符串。 In order to get it to send json you must first convert the dictionary to a string. 为了使它发送json,您必须首先将字典转换为字符串。

Try using: 尝试使用:

JSON.stringify({
        prefix: request,
        locale: '{{locale}}'
})

You're double-encoding your data, actually. 实际上,您正在对数据进行双重编码。 Since you've indicated that the datetype is Json, all you have to do is provide a normal JS data structure: 由于您已指出datetype为Json,因此您所要做的就是提供一个普通的JS数据结构:

data: {
        prefix: request,
        locale: '{{locale}}'
},

and jquery will take care of the stringification for you. 并且jquery将为您处理字符串化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM