首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Appengine ProtoRPC无法解码我的JSON

Appengine ProtoRPC无法解码我的JSON
EN

Stack Overflow用户
提问于 2012-03-27 03:08:24
回答 2查看 742关注 0票数 0

似乎不能让protoRPC应用程序接口在应用引擎上为我工作。

这是我的请求:

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

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

代码语言:javascript
复制
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

这是服务器上的错误:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-27 04:57:52

如果您查看“请求有效负载”部分,就可以看到这里发生的问题。事实证明,当您使用.ajax发送请求时,它不会将外部的'{‘和'}’放在您的请求中。另外,请注意,这些值由&分隔。这意味着.ajax将你的漂亮对象变成了一个you编码的请求,这并不是你想要的。

原因是.ajax "dataType“参数只是指.ajax函数将如何处理请求内容,而不是发送时它将如何处理内容。.ajax始终发送查询字符串。为了让它发送json,您必须首先将字典转换为字符串。

尝试使用:

代码语言:javascript
复制
JSON.stringify({
        prefix: request,
        locale: '{{locale}}'
})
票数 3
EN

Stack Overflow用户

发布于 2012-03-27 03:15:02

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

代码语言:javascript
复制
data: {
        prefix: request,
        locale: '{{locale}}'
},

jquery将为您处理字符串。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9878180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档