2012-03-01 52 views
2

谁能帮我出这个问题,我的JavaScript有一个Ajax GET HTTP请求:用ajax请求和Python的交互谷歌应用程序引擎

$.ajax({ 
    url:"/testPage", 
    type:'GET', 
    success: function(){ 
     alert("done"); 
    } 
}); 

服务器端Python应用程序有一个处理程序来处理HTTP请求从JS:

class testPageHandler(webapp.RequestHandler): 
    def get(self): 
     path=os.path.join(os.path.dirname(_file_).'page1.html') 
     template_values={} 
     self.response.out.write(template.render(path,template_values)) 
    def post(self): 
     ..... 
application=webapp.WSGIApplication([('/testPage',testPageHandler), 
     ..... 

在“得到”的方法,我想Django模板“page1.html”被渲染,所以浏览器显示“page1.html”页面,而不是仅仅弹出“完成”。 有什么想法?提前致谢。

+1

这是一个陈述,而不是一个问题。 – 2012-03-01 01:04:25

+0

对不起,我正在编辑这个问题,你只是早一点。 – cnherald 2012-03-01 01:24:56

+0

重复的http://stackoverflow.com/questions/3430877/how-do-i-redirect-a-page-jquery – 2012-03-01 01:28:43

回答

3

Django模板实际上是作为响应主体呈现和返回的。现在你只想在客户端处理它。

$.ajax({ 
    url:"/testPage", 
    type:'GET', 
    success: function(html){ 
     $('body').append(html); 
    } 
}); 

你可以用任何你喜欢的方式来操作响应。在上面的例子中,它只是附加到body标签。

+0

优秀的答案,我不知道渲染模板可以从服务器响应ajax请求。这也澄清了我在Felix的评论中无法理解的一些细节。感谢你Maxim。 – cnherald 2012-03-01 13:07:17