How to use google app engine with ajax (json)?
google-app-engine
Solution
as long as I know `self.request.body` wouldn't return anything. There's no argument named 'body' in your query-string, but I might be wrong. So, if it returns something, this something is a STRING. So `simplejson.dumps()` cannot turn it into a valid JSON.
If you need a 'list' of all arguments you have sent to the server, use `self.request.arguments()`
`self.response.out.write('Hello, webapp World!')` do not send a valid JSON back to client. It sends a string with "application/json" header instead of "plain/text". Try to create a python dictionary. For example:
`my_response = {'ajax_resp':'Hello, webapp World!'} json = json.dumps(my_resposne)`
and then
`self.response.headers.add_header('content-type', 'application/json', charset='utf-8') self.response.out.write(json) `
On the client side I would suggest you to use console.log() (debugging tool) for testing your responses.
you can just try: `
$.ajax({
type: 'GET',
url: '/AJAX', // or your absolute-path
data : name=totty&age=20,
dataType : 'json',
success : function(resp)
{
console.info("Ajax Response is there.....");
console.log(resp);
}
});
`
Problem
How to use google app engine with ajax (json)? Now I have this but I got this error: ``` raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app import simplejson as json class AjaxHandler(webapp.RequestHandler): def post(self): args = json.loads(self.request.body) self.response.headers['Content-Type'] = 'application/json' self.response.out.write('Hello, webapp World!') application = webapp.WSGIApplication( [('/AJAX', AjaxHandler)], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main() ``` and javascript + jquery: ``` var Server = function() { }; Server.prototype = { init: function(ajaxTargetUrl) { this.ajaxTargetUrl = ajaxTargetUrl; }, request: function(service, data) { $.ajax({ url: this.ajaxTargetUrl, context: document.body, success: function(data) { $('body').append('<p>'+data+'</p>'); }, error: function(){ $('body').append('<p>error</p>'); }, data: data, type: 'POST', dataType: 'json' }); } }; var APP = ( function() { var server = new Server(); server.init('http://localhost:9999/AJAX'); server.request('questions.all', {test:'hey', y:99}); }()); ``` my self.request.body = str: test=hey&y=99