如何使用谷歌应用程序引擎与Ajax(JSON)? [英] How to use google app engine with ajax (json)?
问题描述
如何使用谷歌应用程序引擎与Ajax(json)?
现在我有这个,但我得到这个错误:
raise ValueError(& quot;没有JSON对象可以被解码)
ValueError:不能解码JSON对象
from google.appengine.ext从google.appengine.ext.webapp.util导入webapp
导入run_wsgi_app
将simplejson导入为json
类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()
和javascript + jquery:
var Server = function (){
};
Server.prototype = {
init:function(ajaxTargetUrl){
this.ajaxTargetUrl = ajaxTargetUrl;
},
request:function(service,data){
$ .ajax({
url:this.ajaxTargetUrl,
context:document.body,
成功:函数(数据){
$('body')。append('< p> + data +'< / p>);
},
错误:函数(){
$('body')。append('< p>错误< / p>');
},
data:data,
type :'POST',
dataType:'json'
});
}
};
$ b 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
-
只要我知道
self.request.body
不会返回任何东西。您的查询字符串中没有名为body的参数,但我可能是错的。所以,如果它返回一些东西,这是一个STRING。因此,simplejson.dumps()
无法将其转换为有效的JSON。
$ b 如果您需要'list'所有你发送给服务器的参数,使用self.request.arguments()
-
self.response.out.write('Hello,webapp World!')
不要将有效的JSON发送回客户端。它使用application / json标题而不是plain / text来发送字符串。尝试创建一个Python字典。例如:
my_response = {'ajax_resp':'Hello,webapp World!'}
json = json .dumps(my_resposne)
然后
self.response.out.write(json)
self.response.headers.add_header('content-type','application / json',charset ='utf-8')在客户端,我建议你使用console.log()(调试工具)来测试你的响应。
您可以尝试:
$ .ajax({
type :'GET',
url:'/ AJAX',//或您的绝对路径
data:name = totty& age = 20,
dataType:'json',
success:function(resp)
{
console.info(Ajax Response is there .....);
console.log(resp);
}
});
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
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. Sosimplejson.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); } });
这篇关于如何使用谷歌应用程序引擎与Ajax(JSON)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!