Reputation: 1
var body = JSON.stringify(params);
// Create an XMLHttpRequest 'POST' request w/ an optional callback handler
req.open('POST', '/rpc', async);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", body.length);
req.setRequestHeader("Connection", "close");
if (async) {
req.onreadystatechange = function() {
if(req.readyState == 4 && req.status == 200) {
var response = null;
try {
response = JSON.parse(req.responseText);
} catch (e) {
response = req.responseText;
}
callback(response);
}
};
}
// Make the actual request
req.send(body);
---- on the server side ----
class RPCHandler(BaseHandler):
'''@user_required'''
def post(self):
RPCmethods = ("UpdateScenario", "DeleteScenario")
logging.info(u'body ' + self.request.body)
args = simplejson.loads(self.request.body)
---- Get the following error on the server logs body %5B%22UpdateScenario%22%2C%22c%22%2C%224.5%22%2C%2230frm%22%2C%22Refinance%22%2C%22100000%22%2C%22740%22%2C%2294538%22%2C%2250000%22%2C%22owner%22%2C%22sfr%22%2C%22Fremont%22%2C%22CA%22%5D=
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/google/appengine/ext/webapp/_webapp25.py", line 703, in call handler.post(*groups) File "/base/data/home/apps/s~mortgageratealert-staging/1.357912751535215625/main.py", line 418, in post args = json.loads(self.request.body) 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)
--- firebug shows the following ---
Parameters application/x-www-form-urlencoded
["UpdateScenario","c","4....
Source
["UpdateScenario","c","4.5","30frm","Refinance","100000","740","94538","50000","owner","sfr","Fremont","CA"]
Based on the firebug report and also the logs shows self.request.body as anticipated. However simplejson load doesn't like it.
Please help!
Upvotes: 0
Views: 2370
Reputation: 11706
You do not send JSON content. You have to add this header : contentType: 'application/json'
Upvotes: 0
Reputation: 3651
TWO OPTIONS:
It seems that you need to escape the contents of self.request.body.
Import urllib and change the last line of code in your sample to:
args = simplejson.loads(urllib.unquote(self.request.body))
Or you can try this other option: You are sending a complete json string in the POST body. You might not need to have the following line in your javascript:
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
Take it out, and see if you still need to use the urllib.unquote solution above.
Upvotes: 2