0

似乎无法让 app-engine 上的 protoRPC API 为我工作。

这是我的要求:

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

这是我根据浏览器调试器发送的内容:

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

这是服务器上的错误:

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
4

2 回答 2

3

如果您查看“请求有效负载”部分,可以看到这里发生的问题。事实证明,当您使用 .ajax 发送请求时,它不会在您的请求中放置外部 '{' 和 '}'。另外,请注意这些值由 & 分隔。这意味着 .ajax 将您的好对象转换为 urlencoded 请求,这不是您想要的。

原因是 .ajax 中的“dataType”参数仅指 .ajax 函数将如何处理请求内容,而不是在发送时对内容做什么。.ajax 总是发送一个查询字符串。为了让它发送 json,您必须首先将字典转换为字符串。

尝试使用:

JSON.stringify({
        prefix: request,
        locale: '{{locale}}'
})
于 2012-03-26T20:57:52.430 回答
0

实际上,您正在对数据进行双重编码。既然你已经指出日期类型是 Json,你所要做的就是提供一个普通的 JS 数据结构:

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

jquery 将为您处理字符串化。

于 2012-03-26T19:15:01.910 回答