简单的Python和Ajax示例如何发送响应与Python? [英] Simple Python and Ajax Example How to Send Response with Python?

查看:193
本文介绍了简单的Python和Ajax示例如何发送响应与Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了一些code与Python和JavaScript试图让一个Ajax体系的建立。基本上,我只是想输入一个词,有蟒蛇code发送回。这里是我的HTML / JavaScript的:

I am testing out some code with Python and Javascript trying to get an Ajax system set up. Basically I just want to input a word and have the python code send it back. Here is my html/javascript:

<html>
<head>
<title>Simple Ajax Example</title>
<script language="Javascript">
function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari/Chrome
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(getquerystring());
}

function getquerystring() {
    var form     = document.forms['f1'];
    var word = form.word.value;
    qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage(str){
    document.getElementById("result").innerHTML = str;
}
</script>
</head>

<body>

<form name="f1">
  <p>word: <input name="word" type="text">  
  <input value="Go" type="button" onclick='JavaScript:xmlhttpPost("/ajaxtest")'></p>
  <div id="result"></div>
</form>

</body>

</html>

这里是我的Python:

and here is my python:

class AjaxTest(BlogHandler):
    def get(self):
        user = self.get_user()       
        self.render('ajaxtest.html', user = user)
    def post(self):
        user = self.get_user()
        word = self.request.get('w')
        logging.info(word)
        return '<p>The secret word is' + word + '<p>'
        #having print instead of return didn't do anything

当我登录这个词正确地显示出来,当我在硬code STR:

When I do logging the word shows up correctly and when I hardcode str in:

function updatepage(str){
    document.getElementById("result").innerHTML = str;
}

它显示正常,但现在没有硬编码显示什么。我怎么发送响应?我使用webapp2为我的Python框架的Jinja2的模板引擎,但我​​不认为有很多用它做。我需要的HTTP标头发送?

It displays that correctly but right now without hardcoding it shows nothing. How am I supposed to send the response? I am using webapp2 as my Python framework and Jinja2 as the templating engine, though I don't think that has much to do with it. Do I need to send the HTTP headers?

推荐答案

如果你的问题是有困难的,从您的文章的方法返回一个字符串,勿使您可以使用模板的的方法来实现这一目标:

If your problem is having difficulty returning a string from your post method, without rendering a template you can use the write method to accomplish that:

self.response.write('')

我相信你可以通过只修改改标题 self.response.headers

I believe you can change headers by just modifying self.response.headers

这篇关于简单的Python和Ajax示例如何发送响应与Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆