在我对GAE进行POST后,我想渲染HTML页面,该页面将显示我刚发布的数据。因此,在POST请求中,最后,我使用实体ID调用GET请求。它一直有效,直到我用AJAX请求(而不仅仅是HTML表单)实现POST请求,但现在它不起作用
这是无效的请求:self.redirect('/house/%s' % str(p.key.id()))
。我的直觉说,这是因为我正在从客户端的AJAX函数调用它,并且它试图在AJAX函数完成之前转到新页面。但是,我不是很擅长JS或Python,所以任何帮助都会非常感激。谢谢!终端正在回复此问题:
Magikarp uses Splash. It's not very effective.
INFO 2017-10-24 04:03:00,931 module.py:832] default: "POST /house HTTP/1.1" 302 -
INFO 2017-10-24 04:03:00,994 module.py:832] default: "GET /house/4554452040123423436 HTTP/1.1" 200 3024
def post(self):
if not self.user:
self.redirect('/home')
data = json.loads(self.request.body)
city = data["city"]
state = data["state"]
jsObject = data["jsObject"]
if city and state:
p = Post(parent = blog_key(), city = city, state = state, jsObject = jsObject)
p.put()
print ("Magikarp uses Splash. It's not very effective.")
self.redirect('/house/%s' % str(p.key.id()))
else:
error = "city and state, please!"
self.render("newhouse.html", city=city, state=state, error=error)
function postRequest() {
var city = $("#city").val();
var state = $("#state").val();
var jsObject = JSObject;
$.ajax({
type: "POST",
url: "/submit",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
"city": city,
"state": state,
"jsObject": jsObject
}),
dataType: "json"
})
.done(function(jsonResponse) {
$("#error").html(jsonResponse.message);
});
}